bashcommand-substitution

Backticks vs braces in Bash


When I went to answer this question, I was going to use the ${} notation, as I've seen so many times on here that it's preferable to backticks.

However, when I tried

joulesFinal=${echo $joules2 \* $cpu | bc}

I got the message

-bash: ${echo $joules * $cpu | bc}: bad substitution

but

joulesFinal=`echo $joules2 \* $cpu | bc`

works fine. So what other changes do I need to make?


Solution

  • The `` is called Command Substitution and is almost equivalent to $() (parenthesis), while you are using ${} (curly braces).

    So all of these expressions are equal and mean "interpret the command placed inside":

    joulesFinal=`echo $joules2 \* $cpu | bc`
    joulesFinal=$(echo $joules2 \* $cpu | bc)
    #            v                          v
    #      ( instead of {                   v
    #                                 ) instead of }
    

    While ${} expressions are used for variable substitution.

    Note, though, that backticks are deprecated, while $() is POSIX compatible, so you should prefer the latter.


    From man bash:

    Command substitution allows the output of a command to replace the command name. There are two forms:

              $(command)
       or
              `command`
       
    

    # later on in the man page:
    When the old-style backquote form of substitution is used, backslash retains its literal meaning except when followed by $, `, or \. The first backquote not preceded by a backslash terminates the command substitution. When using the $(command) form, all characters between the parentheses make up the command; none are treated specially.

    Also, `` are more difficult to handle, you cannot nest them for example. See comments below and also Why is $(...) preferred over ... (backticks)?.