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.

Will
  • 1,127
  • 10
  • 25
  • 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`). – Gordon Davisson Dec 21 '15 at 07:11
  • Yes, you completely right, i just used spaces around "=" to make the expression clearly readable. – Dima Zyuryaev Dec 21 '15 at 13:13

1 Answers1

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.

Déjà vu
  • 5,408
  • 9
  • 32
  • 52