Shell – the difference between echo `date`, echo “`date`”, and echo ‘`date`’

echoquotingshell

What is the difference between these three commands?

echo `date`
echo "`date`"
echo '`date`'

I am confused on what the differences actually are. I think that when the ' are around it means that it is a string, therefore echo would literally output the string date instead of displaying the date?

Best Answer

`date` will just expand to the output of the date command. However, it removes extra space characters at places where there are more than one consecutive space character in the output. (This is because the command substitution is subject to word splitting, and because of how the echo command handles multiple arguments.)

In "`date`", the double quotes are weak quotes, so they will expand variables (try "$PWD") and perform command substitution. The result of the expansion is passed as a single argument to the echo command, with any consecutive spaces included: that is, word splitting is not performed.

In '`date`', the single quotes are stronger quotes, so they will not allow expansion of variables or command substitution within them.

Refer this link for more explanation.

Edited the first point as correctly pointed by Michael Suelmann in the comment below.

Related Question