63

I see two different forms in Bash scripts which seem to do the same:

`some command`

and

$(some command)

What is the difference between the two, and when should I use each one of them?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746

1 Answers1

59

There isn't any semantic difference. The backtick syntax is the older and less powerful version. See man bash, section "Command Substitution".

If your shell supports the $() syntax, prefer it because it can be nested.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
thiton
  • 35,651
  • 4
  • 70
  • 100
  • 2
    I see that backticks can also be nested. For example: `\`echo \\`foo\\`\`` – Misha Moroshko Jan 20 '12 at 22:03
  • 12
    @MishaMoroshko: Expressions like `\`echo \`foo\`\` (as opposed to `$(echo $(foo))`) won't work in general because of the inherent ambiguity because each `\`` can be opening or closing. It might work for special cases due to luck or special features. – thiton Jan 20 '12 at 22:06
  • 7
    There _is_ a semantic difference: backtick syntax handles backslashes in a different and non-obvious manner. See: http://mywiki.wooledge.org/BashFAQ/082 – benkc Nov 11 '16 at 20:04
  • 1
    I am using a makefile. In that makefile I have the following command gcc `pkg-config --cflags gtk+-3.0` -o $@ $< `pkg-config --libs gtk+-3.0` If I replace the backticks to the bash function call: gcc $(pkg-config --cflags gtk+-3.0) -o $@ $< $(pkg-config --libs gtk+-3.0) the makefile fails So, via the make utility, there is a difference. The command line execution does not fail. – Leslie Satenstein Aug 20 '17 at 18:15
  • 2
    Another difference is: `echo foo \`#comment\`` vs `echo foo $(#comment)`. The second one doesn't work. (Used for commenting in a multi-line command.) – wisbucky Jul 10 '19 at 22:04