Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
(pedantry follows) Note that in bash, you can replace $(cat file) with $(< file) that is performed entirely in the shell (saving precious microseconds otherwise wasted spawning a cat process). –  glenn jackman Feb 21 '12 at 18:45
1  
(pedantry continued) Never, ever, ever use for to parse something line-by-line. –  grawity Feb 21 '12 at 18:51
    
@grawity - this is a contrivved example entirely to showcase the difference in the two approaches –  warren Feb 21 '12 at 18:52
3  
Also, BashFAQ/082 regarding this question. –  grawity Feb 21 '12 at 18:54

1 Answer 1

up vote 12 down vote accepted

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 ;).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.