0

Using GNU bash 4.1.2, I get:

[roxma@VM_6_207_centos ~]$ echo `echo '\\'`
\
[roxma@VM_6_207_centos ~]$ echo $(echo '\\')
\\
John1024
  • 109,961
  • 14
  • 137
  • 171
rox
  • 525
  • 7
  • 16

1 Answers1

2

The difference is documented in man bash:

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.

As per man bash, a similar difference between the two forms is found if $ follows the backslash:

$ echo `echo 'out: \$'`
out: $
$ echo $(echo 'out: \$')
out: \$

And the same if a backtick follows the backslash:

$ echo `echo 'out: \`'`
out: `
$ echo $(echo 'out: \`')
out: \`

Motivation

Since it could be useful to put a backtick as a character inside the command substitution, the backtick form has to have a way to escape a backtick. To make sure that one can put the escape character whereever one wants, then there also needs to be a way to escape the escape.

John1024
  • 109,961
  • 14
  • 137
  • 171
  • 4
    In other words: backquotes have weird quoting/escaping rules, while `$()` parses its contents normally. Also, `$()` is much easier for a human to read. So use `$()`. – Gordon Davisson Aug 29 '15 at 07:07