54

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.

coolcric
  • 651
  • 10
    Note that backticks are not recommended since you will lose the ability to do nested execute substitutions. Insetad, $(somecommand) is preferred. Imho it improves readability. – Tommy Sep 17 '12 at 13:11
  • 6
    @Tommy, No, backticks can be nested, but it becomes an escaping nightmare, especially when double-quoting as well. – Stéphane Chazelas Sep 22 '12 at 09:13

3 Answers3

75

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.

  • 1
    Hi @Krzysztof, that is helpful! "since $b contains 5, bash is trying to run command 5" is exactly what I was looking for – coolcric Sep 17 '12 at 11:58
  • you can use quotation marks, like echo "$b" => does the "" do anything special? It seems echo $b would work just as well. – Tootsie Rolls Jul 07 '13 at 22:01
  • @Anita: In general quotation marks does some special things (it changes the way special characters are treated - especially white spaces) but in this case, they can be dropped without much problems. This is because no special characters are used and echo command don't care about the number of arguments it gets so if b has some spaces inside, it will get multiple arguments and will still print them. This may no be the case for other commands (especially when variable is not set) so I tend to always surround variables with quotes. – Krzysztof Adamski Jul 08 '13 at 06:59
  • would be nice if (POSIX) would be a link to the actual standard. – erikbstack May 22 '15 at 11:38
  • 2
    @erikb85: This note was not added by me but I've edited it with the link as you suggested :) – Krzysztof Adamski May 22 '15 at 12:09
12

The backtick does exactly what you say it does. You have set a variable to an integer. When you put that variable inside backticks, bash will try to execute it as a command. Since it is not a command, you get the error you saw.

What you want to do is simply:

$ b=5; echo "$b"

To understand backticks better, compare with:

$ b=5; a=`echo "$b"`; echo "$a"
5
terdon
  • 242,166
9

Going step by step your line should explain it.

$ b=5; echo `$b`;
  1. sets variable b to 5
  2. evaluates $b (effectively runs 5)
  3. echoes the output of the evaluation above.

So yes, the output you got is expected. You're evaluating the contents of a variable, not the actual command you thought you were. Everything you put inside backticks is simplay evaluated (runned) in a new (sub)shell.

chepner
  • 7,501
gertvdijk
  • 13,977
  • hi @gertvdijk, thanks for your post! I was thinking that returns whatever it is left with after evaluating its contents. So, I thought$b' would have returned 5 for echo to print it – coolcric Sep 17 '12 at 12:02