Differences between revisions 20 and 28 (spanning 8 versions)
Revision 20 as of 2012-06-28 04:40:08
Size: 3071
Editor: ormaaj
Comment: Some people are stubborn. Make it convincing.
Revision 28 as of 2021-07-18 10:40:15
Size: 3229
Editor: geirha
Comment: avoid treating a path as regex
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
Line 6: Line 7:
Line 8: Line 8:
  {{{
  
$ 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`" "$(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 \\
}}}
Line 19: Line 19:
  {{{
  
echo "x is $(echo "$y" | sed ...)
  }}}
 
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,
  {{{
  
echo "x is `echo \"$y\" | sed ...`"
  }}}
 
requires backslashes around the internal quotes in order to be portable. Bourne and Korn shells require these backslashes, while Bash and dash don't.
 {{{#!highlight bash
echo "x is $(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,
 {{{#!highlight bash
echo "x is `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.
Line 29: Line 29:
  {{{
  x=$(grep "$(dirname "$path")" file)
  x=`grep "\`dirname "$path"\`" file`
  }}}
  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.
 {{{#!highlight bash
x=$(grep -F "$(dirname "$path")" file)
x=`grep -F "\`dirname \"$path\"\`" file`
}}}
 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.
Line 36: Line 36:
Line 44: Line 43:
 * [[http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_03|POSIX standard and section "2.6.3 Command Substitution"]]
 * [[CommandSubstitution]]
 * [[http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_03|POSIX standard section "2.6.3 Command Substitution"]]
 * [[http://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xcu_chap02.html#tag_23_02_06_03|POSIX rationale for including the $() syntax]]
 * CommandSubstitution

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

`...` is the legacy syntax required by only the very oldest of non-POSIX-compatible bourne-shells. There are several reasons to always prefer the $(...) syntax:

Important differences

  • Backslashes (\) inside backticks are handled in a non-obvious manner:
       1 $ echo "`echo \\a`" "$(echo \\a)"
       2 a \a
       3 $ echo "`echo \\\\a`" "$(echo \\\\a)"
       4 \a \\a
       5 # Note that this is true for *single quotes* too!
       6 $ foo=`echo '\\'`; bar=$(echo '\\'); echo "foo is $foo, bar is $bar"
       7 foo is \, bar is \\
    
  • Nested quoting inside $() is far more convenient.

       1 echo "x is $(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 "x is `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=$(grep -F "$(dirname "$path")" file)
       2 x=`grep -F "\`dirname \"$path\"\`" file`
    

    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 2022-02-19 00:13:59 by larryv)