Differences between revisions 2 and 17 (spanning 15 versions)
Revision 2 as of 2007-05-09 18:20:43
Size: 792
Editor: GreyCat
Comment:
Revision 17 as of 2010-11-04 08:36:31
Size: 2069
Editor: finproxy
Comment: Add POSIX URL
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[Anchor(faq82)]] <<Anchor(faq82)>>
Line 3: Line 3:
Line 6: Line 5:
 * It's easier to read. The character {{{`}}} is difficult to read with small or unusual fonts.

 * It's easier to type. The physical key to produce the character may be located in an obscure place on keyboards, or may not be present at all (like in the standard italian keyboard).

 * The backtick is easily confused with a single quote. People who see {{{$()}}} don't normally press the wrong keys. On the other hand, some people who see {{{`cmd`}}} may mangle it into {{{'cmd'}}} because they don't know what a backtick is.
Line 7: Line 11:
Line 11: Line 14:
Line 14: Line 16:
 * It's easier to read.
 * Newbies who see {{{$()}}} don't normally press the wrong keys. On the other hand, newbies who see {{{`cmd`}}} often mangle it into {{{'cmd'}}} because they don't know what a backtick is.
 * Backslashes (\) inside backticks are handled in a non-obvious manner. ''(Example desired!)'' Inside {{{$()}}}, there are no such surprises.
 * Backslashes (\) inside backticks are handled in a non-obvious manner:
  {{{
  $ 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 18: Line 27:
The only time backticks are preferred is when writing code for the oldest Bourne shells, which do not know about {{{$()}}}.  * Nested quoting inside {{{$()}}} is far more convenient.
  {{{
  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.

The only time backticks are preferred is when writing code for the oldest Bourne shells, which are not POSIx compliant. See [http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_03 POSIX standard and section "command substitution2] for {{{$()}}}.

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

For several reasons:

  • It's easier to read. The character ` is difficult to read with small or unusual fonts.

  • It's easier to type. The physical key to produce the character may be located in an obscure place on keyboards, or may not be present at all (like in the standard italian keyboard).
  • The backtick is easily confused with a single quote. People who see $() don't normally press the wrong keys. On the other hand, some people who see `cmd` may mangle it into 'cmd' because they don't know what a backtick is.

  • It makes nesting command substitutions easier. Compare:
    •   x=$(grep $(dirname "$path") file)
        x=`grep \`dirname "$path"\` file`
    It just gets uglier and uglier after two levels.
  • Backslashes (\) inside backticks are handled in a non-obvious manner:
    •   $ 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 \\
  • Nested quoting inside $() is far more convenient.

    •   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.

The only time backticks are preferred is when writing code for the oldest Bourne shells, which are not POSIx compliant. See [http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_03 POSIX standard and section "command substitution2] for $().

BashFAQ/082 (last edited 2022-02-19 00:13:59 by larryv)