The difference between `command` and $(command)

The old style backticks ` ` and the $( ) constructions do the same thing, but treat the backslashes different.

Using backticks:

$ echo `echo test`
test

Using $( ):

$ echo $(echo test)
test

In the $( ) construction, everything in between ( ) is interpreted as a command.

As we can notice, the two tricks behave different when interpreting special characters:

With $( ):
$ echo $(echo '\\')
\\

With backticks:
echo `echo '\\'`
\

In the second construction, the “backslash” \ escapes the second one, while in the first, the sell interprets it as normal character an prints two “backslashes” \\ .

I avoid using the backticks, because they can easily be confused with the normal ticks ‘ ‘.

All the new shells support both the ` ` and %( ) constructions. The old bash does not know how to interpret the $( ) trick.

Tagged with: , , , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Subscribe

  

Subscribe to get the latest Linux news and how to guides directly on your e-mail!