1707
Comment: converted to 1.6 markup
|
1849
|
Deletions are marked like this. | Additions are marked like this. |
Line 3: | Line 3: |
Line 8: | Line 7: |
* It's easier to type. The physical key to produce the character may be located in an obscure place on non-US keyboards. | * It's easier to type. The physical key to produce the character may be located in an obscure place on keyboards. |
Line 23: | Line 22: |
\a \\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 33: | Line 36: |
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.
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`
- 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 ...`"
The only time backticks are preferred is when writing code for the oldest Bourne shells, which do not know about $().