Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 bash? seems to say that ` evaluates the commands within and replaces them with the output.

share|improve this question
6  
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
3  
@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 9:13

3 Answers 3

up vote 20 down vote accepted

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 that syntax is that 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 suported 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.

share|improve this answer
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 7 '13 at 22:01
    
@Anita: In general quotation marks does some special things (it changes the way special characters are threaten - 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 8 '13 at 6:59

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
share|improve this answer

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|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.