Size: 792
Comment:
|
Size: 895
Comment: Add example for non-obvious backslashing behaviour
|
Deletions are marked like this. | Additions are marked like this. |
Line 16: | Line 16: |
* 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. |
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'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.
The only time backticks are preferred is when writing code for the oldest Bourne shells, which do not know about $().