Bash – Understanding Backtick (`) in Command Substitution

bashcommand-substitutionshell

I am trying out the command

$ b=5; echo `$b`;
-bash: 5: command not found

but it does not print 5 as it is supposed to. What am I missing here?

What does ` (backquote/backtick) mean in commands? seems to say that ` evaluates the commands within and replaces them with the output.

Best Answer

Text between backticks is executed and replaced by the output of the command (minus the trailing newline characters, and beware that shell behaviors vary when there are NUL characters in the output). That is called command substitution because it is substituted with the output of the command. So if you want to print 5, you can't use backticks, you can use quotation marks, like echo "$b" or just drop any quotation and use echo $b.

As you can see, since $b contains 5, when using backticks bash is trying to run command 5 and since there is no such command, it fails with error message.

To understand how backticks works, try running this:

$ A=`cat /etc/passwd | head -n1`
$ echo "$A"

cat /etc/passwd |head -n1 should print first line of /etc/passwd file. But since we use backticks, it doesn't print this on console. Instead it is stored in A variable. You can echo $A to this. Note that more efficient way of printing first line is using command head -n1 /etc/passwd but I wanted to point out that expression inside of backticks does not have to be simple.

So if first line of /etc/passwd is root:x:0:0:root:/root:/bin/bash, first command will be dynamically substituted by bash to A="root:x:0:0:root:/root:/bin/bash".

Note that this syntax is of the Bourne shell. Quoting and escaping becomes quickly a nightmare with it especially when you start nesting them. Ksh introduced the $(...) alternative which is now standardized (POSIX) and supported by all shells (even the Bourne shell from Unix v9). So you should use $(...) instead nowadays unless you need to be portable to very old Bourne shells.

Also note that the output of `...` and $(...) are subject to word splitting and filename generation just like variable expansion (in zsh, word splitting only), so would generally need to be quoted in list contexts.