Differences between revisions 2 and 5 (spanning 3 versions)
Revision 2 as of 2007-05-09 18:20:43
Size: 792
Editor: GreyCat
Comment:
Revision 5 as of 2007-09-14 13:13:13
Size: 2511
Editor: GreyCat
Comment: several changes
Deletions are marked like this. Additions are marked like this.
Line 7: Line 7:
Line 11: Line 10:
Line 15: Line 13:
Line 16: Line 15:
 * 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`" # prints a
  echo "`echo \\\a`" # prints \a
  echo "`echo \\\\a`" # prints \a}}}
 Inside {{{$()}}}, there are no such surprises.

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

Backslashes are no more no less surprising than elsewhere IMHO
{{{
  echo `echo \a` # prints a
  echo `echo \\a` # prints a
  echo `echo \\\a` # prints \a
  echo `echo \\\\a` # prints \a
  echo $(echo \a) # prints a
  echo $(echo \\a) # prints \a
  echo $(echo \\\a) # prints \a
  echo $(echo \\\\a) # prints \\a
}}}
The same sort of things happens without any quotes or within "".

I suspect the real advantage of $( ) here is that you don't need to take extra care of the quotes (\ ""), you just put them as usual, ie
{{{echo "`echo \"foo bar\"`"}}} vs {{{echo "$( echo "foo bar")"}}} -- pgas
 ''Just for the record, \" inside {{{`backticks`}}} is only required in Korn shell and Bourne shell. Bash and dash both treat''
 {{{
 echo "`echo "foo bar"`"}}}
 ''exactly the same way they treat''
 {{{
 echo "$(echo "foo bar")"}}}
 ''although this is an excellent point that I'm going to add to the bullet list up above.'' -- GreyCat

Anchor(faq82)

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

For several reasons:

  • 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.
  • 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:
    •   echo "`echo \\a`"      # prints a
        echo "`echo \\\a`"     # prints \a
        echo "`echo \\\\a`"    # prints \a

    Inside $(), there are no such surprises.

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

Backslashes are no more no less surprising than elsewhere IMHO

  echo `echo \a`     # prints a
  echo `echo \\a`    # prints a
  echo `echo \\\a`   # prints \a
  echo `echo \\\\a`  # prints \a
  echo $(echo \a)    # prints a
  echo $(echo \\a)   # prints \a
  echo $(echo \\\a)  # prints \a
  echo $(echo \\\\a) # prints \\a

The same sort of things happens without any quotes or within "".

I suspect the real advantage of $( ) here is that you don't need to take extra care of the quotes (\ ""), you just put them as usual, ie echo "`echo \"foo    bar\"`" vs echo "$( echo "foo     bar")" -- pgas

  • Just for the record, \" inside `backticks` is only required in Korn shell and Bourne shell. Bash and dash both treat

     echo "`echo "foo       bar"`"

    exactly the same way they treat

     echo "$(echo "foo       bar")"

    although this is an excellent point that I'm going to add to the bullet list up above. -- GreyCat

The only time backticks are preferred is when writing code for the oldest Bourne shells, which do not know about $().

BashFAQ/082 (last edited 2021-08-10 13:22:46 by GreyCat)