Alex Rivera | Logout

What is the difference between $(command) and `command` in shell programming?

Asked 2011-01-16T22:34:37.407
334

To store the output of a command as a variable in sh/ksh/bash, you can do either

var=$(command)

or

var=`command`

What's the difference if any between the two methods?

Edit
Report

2 Answers

44

They behave the same. The difference is syntactical: it's easier to nest $() than ``:

listing=$(ls -l $(cat filenames.txt))

vs.

listing=`ls -l \`cat filenames.txt\``
answered 2011-01-16T22:39:55.623
6

There is little difference, except for what unescaped characters you can use inside of the command. You can even put `...` commands inside $(...) ones (and vice versa) for a more complicated two-level-deep command substitution.

There is a slightly different interpretation of the backslash character/operator. Among other things, when nesting `...` substitution commands, you must escape the inner ` characters with \, whereas with $() substition it understands the nesting automatically.

answered 2011-01-16T22:37:45.317

Your Answer