echo ^`date`
^Wed Jan 21 05:49:37 CST 2015
deeps=`echo ^`date``
echo $deeps
Actual Result:
^date
Expected Result :
^Wed Jan 21 05:49:37 CST 2015
Need help on this
echo ^`date`
^Wed Jan 21 05:49:37 CST 2015
deeps=`echo ^`date``
echo $deeps
Actual Result:
^date
Expected Result :
^Wed Jan 21 05:49:37 CST 2015
Need help on this
Use a backtick, or use Command substitution. Like
or
Both output (the requested)
Try this method
Output :
Backticks are horribly outdated and should not be used any more -- using $() instead will save you many headaches
Simply because neither of the other (correct) answers actually explain the problem I'm adding another answer.
tl;dr Compare the backtick version to the
$()
versionThe difference between
and
is how many backticks are on the line and how the shell is parsing the line.
has a single pair of backticks and the shell parses it as (where the
[]
are marking "parts" of the line)The
bit is then expanded via Command Substitution and so the line becomes
and then
echo
spits out the desired^Wed Jan 21 05:49:37 CST 2015
.This line however
is parsed as
which you can already see is quite different and not correct (this happens because backticks cannot be nested for the record).
There are now two command substitutions on this line
echo ^
and the empty string so the line becomesor with the "words" combined
which then assigns
^date
todeeps
andecho $deeps
then gets you^date
.The
$()
form of command substitution, no the other hand, does nest and thusparses as
which properly runs both
date
andecho
on the result. Though, as indicated in the other answers, the wrappingecho
is not necessary asdeeps=^$(date)
will work just fine by itself.