4

I wonder if there is a significant difference of calling sub shell via $(...) or `...`?

For example:

a=$(ls -la /tmp | grep vox-*)

And:

a=`ls -la /tmp | grep vox-*`

The result will be the completely the same, but I want to know why there are two different methods, what the difference is, and which one I should use.

2
  • 1
    Actually, neither of these will work because you have spaces around the =. In the shell, spaces are forbidden around the = when doing an assignment, but required when doing a test (like if [ "$a" = "$b" ]; then). Dec 21, 2015 at 7:11
  • Yes, you completely right, i just used spaces around "=" to make the expression clearly readable. Dec 21, 2015 at 13:13

1 Answer 1

3

Backsticks and $(...) are identical in terms of functionality. However I prefer the second approach

  • $(...) can be easily nested

  • readability, $(...) is more "bashish"

  • consistency, as a similar syntax, $((...)), is for expressions

See also this page that has other arguments.

1
  • 2
    $( ) is not bashish or à bashism, it was introduced by ksh and is POSIX.
    – jlliagre
    Dec 20, 2015 at 15:57

You must log in to answer this question.

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