Why use parens instead of backticks for executing a command 'in situ'?
I have recently started to shift my shell scripting from utilizing backticks to parens to execute a command in situ and use the results in something else. Eg:
for line in `cat file`
do
echo "$line"
done
Now I use parens, substituting thusly:
for line in $(cat file)
...
What is the actual difference between the two methods, and why is paren substitution considered better than backticks?
Solution 1:
There is no functional difference, however $()
makes nesting a bit nicer and easier to follow. Consider this silly example:
$ echo `echo \`echo \\\`echo foo\\\`\``
foo
vs
$ echo $(echo $(echo $(echo foo)))
foo
Now consider doing it with a complex series of commands that do something useful ;).