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`
    echo "$line"

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?

  • (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. – user1686 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. – user1686 Feb 21 '12 at 18:54

There is no functional difference, however $() makes nesting a bit nicer and easier to follow. Consider this silly example:

$ echo `echo \`echo \\\`echo foo\\\`\``


$ echo $(echo $(echo $(echo foo)))

Now consider doing it with a complex series of commands that do something useful ;).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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