Differences between revisions 32 and 34 (spanning 2 versions)
Revision 32 as of 2025-04-23 14:15:55
Size: 3271
Editor: 81
Comment: Improve leading paragraph
Revision 34 as of 2025-04-23 14:34:15
Size: 3278
Editor: 81
Comment: Update Bash hackers URL to webarchive
Deletions are marked like this. Additions are marked like this.
Line 7: Line 7:
 {{{#!highlight bash
$ echo "`echo \\a`" "$(echo \\a)"
a \a
$ echo "`echo \\\\a`" "$(echo \\\\a)"
\a \\a

# Note that this is true for *single quotes* too!
$ foo=`echo '\\'`; bar=$(echo '\\'); echo "foo is $foo, bar is $bar"
foo is \, bar is \\
{{{#!highlight bash
$ echo "`echo \\a`"
a
$ echo "`echo \\\\a`"
\a
$ echo
"$(echo \\a)"
\\a

# Note that this is true
#
for *single quotes* too!
$ str=`echo '\\'`
$ echo "str is $str"
str is \
$ st
r=$(echo '\\')
$ echo "str is $str"
str is \\
Line 19: Line 27:
echo "x is $(sed ... <<<"$y")" echo "$(sed ... <<<"$y")"
Line 23: Line 31:
echo "x is `sed ... <<<\"$y\"`" echo "`sed ... <<<\"$y\"`"
Line 29: Line 37:
x=$(grep -F "$(dirname "$path")" file)
x=`grep -F "\`dirname \"$path\"\`" file`
x=$(basename "$(pwd)")
x=`basename "\`pwd\`"`
Line 44: Line 52:
 * [[http://wiki.bash-hackers.org/syntax/expansion/cmdsubst|bash-hackers: command substitution]]  * [[https://web.archive.org/web/20230318045013/http://wiki.bash-hackers.org/syntax/expansion/cmdsubst|bash-hackers: command substitution]]

Why is $(...) preferred over `...` (backticks)?

The `cmd` backtick format is the legacy syntax for command substitution, required only by more than 30-year-old Bourne shells. The modern POSIX $(...) syntax is preferred for many reasons:

Important differences

  • Backslashes (\) inside backticks are handled in a non-obvious manner:

   1 $ echo "`echo \\a`"
   2 a
   3 $ echo "`echo \\\\a`"
   4 \a 
   5 $ echo "$(echo \\a)"
   6 \\a
   7 
   8 # Note that this is true
   9 # for *single quotes* too!
  10 $ str=`echo '\\'`
  11 $ echo "str is $str"
  12 str is \
  13 $ str=$(echo '\\')
  14 $ echo "str is $str"
  15 str is \\
  • Nested quoting inside $() is far more convenient.

       1 echo "$(sed ... <<<"$y")"
    

    In this example, the quotes around $y are treated as a pair, because they are inside $(). This is confusing at first glance, because most C programmers would expect the quote before x and the quote before $y to be treated as a pair; but that isn't correct in shells. On the other hand,

       1 echo "`sed ... <<<\"$y\"`"
    
    requires backslashes around the internal quotes in order to be portable. Bourne and Korn shells require these backslashes, while Bash and dash don't.
  • It makes nesting command substitutions easier. Compare:
       1 x=$(basename "$(pwd)")
       2 x=`basename "\`pwd\`"`
    

    It just gets uglier and uglier after two levels. $() forces an entirely new context for quoting, so that everything within the command substitution is protected and can be treated as though it were on its own, with no special concern over quoting and escaping.

Other advantages

  • The function of $(...) as being an expansion is visually clear. The syntax of a $-prefixed token is consistent with all other expansions that are parsed from within double-quotes, at the same time, from left-to-right. Backticks are the only exception. This improves human and machine readability, and consistent syntax makes the language more intuitive for readers.

  • Per the above, people are (hopefully) accustomed to seeing double-quoted expansions and substitutions with the usual "$..." syntax. Quoting command substitutions is almost always the correct thing to do, yet the great majority of `...` specimens we find in the wild are left unquoted, perhaps because those who still use the legacy syntax are less experienced, or they don't associate it with the other expansions due to the different syntax. In addition, the ` character is easily camouflaged when adjacent to " making it even more difficult to read, especially with small or unusual fonts.

  • The backtick is also easily confused with a single quote.

See also:

BashFAQ/082 (last edited 2025-04-27 08:00:55 by StephaneChazelas)