Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .