Bash Command Substitution doesn't work
This is the script:
#!/bin/bash
thedate=$(date)
var='Current date is $thedate'
echo $var
The output is Current date is $thedate and I'd like to make it show the date,what did I do wrong? Any help would be appreciated.
bash script
add a comment |
This is the script:
#!/bin/bash
thedate=$(date)
var='Current date is $thedate'
echo $var
The output is Current date is $thedate and I'd like to make it show the date,what did I do wrong? Any help would be appreciated.
bash script
add a comment |
This is the script:
#!/bin/bash
thedate=$(date)
var='Current date is $thedate'
echo $var
The output is Current date is $thedate and I'd like to make it show the date,what did I do wrong? Any help would be appreciated.
bash script
This is the script:
#!/bin/bash
thedate=$(date)
var='Current date is $thedate'
echo $var
The output is Current date is $thedate and I'd like to make it show the date,what did I do wrong? Any help would be appreciated.
bash script
bash script
asked Dec 7 at 18:36
Iulia Danilov
1
1
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
The problem is that bash expands environment variables only once, unless you use eval, which causes the command line to be parsed twice:
eval echo $var
Note that the date and time displayed are those current when thedate is set, not when $var is referenced. To display the current time when $var is referenced, you need:-
var='Current date is $(date)'
eval echo $var
It would be better not to use variables, but define a function:-
cdate() { echo Current date is $(date); }
...
cdate
Of course, it is better still not to use echo, but let date itself add the extra text:
cdate() { date +"Current date is %c"; }
This doesn't output quite the same format as the date default, but there is no format specifier for the default.
add a comment |
var='Current date is $thedate'
Variables don't expand within single quotes, so this assigns a string containing the literal text $thedate. You should use double quotes here to have the variable expand.
echo $var
Also, here, you should use double quotes around the variable to prevent it from being subject to word splitting and pathname expansion, i.e. echo "$var". In this particular case you can mostly get away with not using quotes, since the date probably will not contain wildcard characters. But without quotes, e.g. the date string Fri Dec 7 20:41:21 EET 2018 would output as Fri Dec 7 20:41:21 EET 2018, that is, the double space after the month name would be collapsed to a single space.
1
If you usevar="Current date is $thedate", then the date and time are set at the timevaris defined, not at the the time$varis referenced.
– AFH
Dec 7 at 18:45
@AFH, at the timethedateis assigned (notvar), since that's where the command substitution is. I don't know when exactly they want to run thedatecommand, since they don't say, and there's the more immediate problem of not even expanding$thedate...
– ilkkachu
Dec 7 at 18:50
Sorry, you're quite right: I was writing my own answer at the time, and responded too quickly.
– AFH
Dec 7 at 18:55
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1381718%2fbash-command-substitution-doesnt-work%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem is that bash expands environment variables only once, unless you use eval, which causes the command line to be parsed twice:
eval echo $var
Note that the date and time displayed are those current when thedate is set, not when $var is referenced. To display the current time when $var is referenced, you need:-
var='Current date is $(date)'
eval echo $var
It would be better not to use variables, but define a function:-
cdate() { echo Current date is $(date); }
...
cdate
Of course, it is better still not to use echo, but let date itself add the extra text:
cdate() { date +"Current date is %c"; }
This doesn't output quite the same format as the date default, but there is no format specifier for the default.
add a comment |
The problem is that bash expands environment variables only once, unless you use eval, which causes the command line to be parsed twice:
eval echo $var
Note that the date and time displayed are those current when thedate is set, not when $var is referenced. To display the current time when $var is referenced, you need:-
var='Current date is $(date)'
eval echo $var
It would be better not to use variables, but define a function:-
cdate() { echo Current date is $(date); }
...
cdate
Of course, it is better still not to use echo, but let date itself add the extra text:
cdate() { date +"Current date is %c"; }
This doesn't output quite the same format as the date default, but there is no format specifier for the default.
add a comment |
The problem is that bash expands environment variables only once, unless you use eval, which causes the command line to be parsed twice:
eval echo $var
Note that the date and time displayed are those current when thedate is set, not when $var is referenced. To display the current time when $var is referenced, you need:-
var='Current date is $(date)'
eval echo $var
It would be better not to use variables, but define a function:-
cdate() { echo Current date is $(date); }
...
cdate
Of course, it is better still not to use echo, but let date itself add the extra text:
cdate() { date +"Current date is %c"; }
This doesn't output quite the same format as the date default, but there is no format specifier for the default.
The problem is that bash expands environment variables only once, unless you use eval, which causes the command line to be parsed twice:
eval echo $var
Note that the date and time displayed are those current when thedate is set, not when $var is referenced. To display the current time when $var is referenced, you need:-
var='Current date is $(date)'
eval echo $var
It would be better not to use variables, but define a function:-
cdate() { echo Current date is $(date); }
...
cdate
Of course, it is better still not to use echo, but let date itself add the extra text:
cdate() { date +"Current date is %c"; }
This doesn't output quite the same format as the date default, but there is no format specifier for the default.
answered Dec 7 at 19:38
AFH
13.9k31938
13.9k31938
add a comment |
add a comment |
var='Current date is $thedate'
Variables don't expand within single quotes, so this assigns a string containing the literal text $thedate. You should use double quotes here to have the variable expand.
echo $var
Also, here, you should use double quotes around the variable to prevent it from being subject to word splitting and pathname expansion, i.e. echo "$var". In this particular case you can mostly get away with not using quotes, since the date probably will not contain wildcard characters. But without quotes, e.g. the date string Fri Dec 7 20:41:21 EET 2018 would output as Fri Dec 7 20:41:21 EET 2018, that is, the double space after the month name would be collapsed to a single space.
1
If you usevar="Current date is $thedate", then the date and time are set at the timevaris defined, not at the the time$varis referenced.
– AFH
Dec 7 at 18:45
@AFH, at the timethedateis assigned (notvar), since that's where the command substitution is. I don't know when exactly they want to run thedatecommand, since they don't say, and there's the more immediate problem of not even expanding$thedate...
– ilkkachu
Dec 7 at 18:50
Sorry, you're quite right: I was writing my own answer at the time, and responded too quickly.
– AFH
Dec 7 at 18:55
add a comment |
var='Current date is $thedate'
Variables don't expand within single quotes, so this assigns a string containing the literal text $thedate. You should use double quotes here to have the variable expand.
echo $var
Also, here, you should use double quotes around the variable to prevent it from being subject to word splitting and pathname expansion, i.e. echo "$var". In this particular case you can mostly get away with not using quotes, since the date probably will not contain wildcard characters. But without quotes, e.g. the date string Fri Dec 7 20:41:21 EET 2018 would output as Fri Dec 7 20:41:21 EET 2018, that is, the double space after the month name would be collapsed to a single space.
1
If you usevar="Current date is $thedate", then the date and time are set at the timevaris defined, not at the the time$varis referenced.
– AFH
Dec 7 at 18:45
@AFH, at the timethedateis assigned (notvar), since that's where the command substitution is. I don't know when exactly they want to run thedatecommand, since they don't say, and there's the more immediate problem of not even expanding$thedate...
– ilkkachu
Dec 7 at 18:50
Sorry, you're quite right: I was writing my own answer at the time, and responded too quickly.
– AFH
Dec 7 at 18:55
add a comment |
var='Current date is $thedate'
Variables don't expand within single quotes, so this assigns a string containing the literal text $thedate. You should use double quotes here to have the variable expand.
echo $var
Also, here, you should use double quotes around the variable to prevent it from being subject to word splitting and pathname expansion, i.e. echo "$var". In this particular case you can mostly get away with not using quotes, since the date probably will not contain wildcard characters. But without quotes, e.g. the date string Fri Dec 7 20:41:21 EET 2018 would output as Fri Dec 7 20:41:21 EET 2018, that is, the double space after the month name would be collapsed to a single space.
var='Current date is $thedate'
Variables don't expand within single quotes, so this assigns a string containing the literal text $thedate. You should use double quotes here to have the variable expand.
echo $var
Also, here, you should use double quotes around the variable to prevent it from being subject to word splitting and pathname expansion, i.e. echo "$var". In this particular case you can mostly get away with not using quotes, since the date probably will not contain wildcard characters. But without quotes, e.g. the date string Fri Dec 7 20:41:21 EET 2018 would output as Fri Dec 7 20:41:21 EET 2018, that is, the double space after the month name would be collapsed to a single space.
answered Dec 7 at 18:42
ilkkachu
590212
590212
1
If you usevar="Current date is $thedate", then the date and time are set at the timevaris defined, not at the the time$varis referenced.
– AFH
Dec 7 at 18:45
@AFH, at the timethedateis assigned (notvar), since that's where the command substitution is. I don't know when exactly they want to run thedatecommand, since they don't say, and there's the more immediate problem of not even expanding$thedate...
– ilkkachu
Dec 7 at 18:50
Sorry, you're quite right: I was writing my own answer at the time, and responded too quickly.
– AFH
Dec 7 at 18:55
add a comment |
1
If you usevar="Current date is $thedate", then the date and time are set at the timevaris defined, not at the the time$varis referenced.
– AFH
Dec 7 at 18:45
@AFH, at the timethedateis assigned (notvar), since that's where the command substitution is. I don't know when exactly they want to run thedatecommand, since they don't say, and there's the more immediate problem of not even expanding$thedate...
– ilkkachu
Dec 7 at 18:50
Sorry, you're quite right: I was writing my own answer at the time, and responded too quickly.
– AFH
Dec 7 at 18:55
1
1
If you use
var="Current date is $thedate", then the date and time are set at the time var is defined, not at the the time $var is referenced.– AFH
Dec 7 at 18:45
If you use
var="Current date is $thedate", then the date and time are set at the time var is defined, not at the the time $var is referenced.– AFH
Dec 7 at 18:45
@AFH, at the time
thedate is assigned (not var), since that's where the command substitution is. I don't know when exactly they want to run the date command, since they don't say, and there's the more immediate problem of not even expanding $thedate...– ilkkachu
Dec 7 at 18:50
@AFH, at the time
thedate is assigned (not var), since that's where the command substitution is. I don't know when exactly they want to run the date command, since they don't say, and there's the more immediate problem of not even expanding $thedate...– ilkkachu
Dec 7 at 18:50
Sorry, you're quite right: I was writing my own answer at the time, and responded too quickly.
– AFH
Dec 7 at 18:55
Sorry, you're quite right: I was writing my own answer at the time, and responded too quickly.
– AFH
Dec 7 at 18:55
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1381718%2fbash-command-substitution-doesnt-work%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown