Understanding backtick (`)

94,935

Solution 1

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.

Solution 2

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

Solution 3

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.

Share:
94,935

Related videos on Youtube

coolcric
Author by

coolcric

Updated on September 18, 2022

Comments

  • coolcric
    coolcric almost 2 years

    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.

    • Admin
      Admin almost 12 years
      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.
    • Admin
      Admin almost 12 years
      @Tommy, No, backticks can be nested, but it becomes an escaping nightmare, especially when double-quoting as well.
  • coolcric
    coolcric almost 12 years
    Hi @Krzysztof, that is helpful! "since $b contains 5, bash is trying to run command 5" is exactly what I was looking for
  • coolcric
    coolcric almost 12 years
    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
  • Krzysztof Adamski
    Krzysztof Adamski almost 11 years
    @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.
  • erikbstack
    erikbstack about 9 years
    would be nice if (POSIX) would be a link to the actual standard.
  • Krzysztof Adamski
    Krzysztof Adamski about 9 years
    @erikb85: This note was not added by me but I've edited it with the link as you suggested :)