Bash - Difference between echo `basename $ HOME` and echo $ (basename $ HOME)

Thank you for your help.

The name says it all: what's the difference between use:

echo `basename $HOME` 

and

 echo $(basename $HOME) 

Note that I know what the basename command does, that both syntaxes are valid, and both commands give the same result.

I'm just wondering if there is a difference between the two, and if possible, why there are two syntaxes for this.

Greetings

Raphael

+4
source share
3 answers

The second form has different shielding rules, which greatly facilitates their embedding. eg.

 echo $(echo $(basename $HOME)) 

I will leave everything to do it, "as an exercise for the reader, it must become enlightened."

+6
source

They are one of the same. please read this .

EDIT (by reference):
Team Replacement

Command substitution allows you to display a command to replace the command itself. Command substitution occurs when a command is enclosed in the following command:

 $(command) 

or so using backlinks:

 `command` 

Bash performs the extension by executing the COMMAND command and replacing the command substitution with the standard output of the command, removing any trailing lines. Inline newlines are not deleted, but they can be deleted during word splitting.

 $ franky ~> echo `date` Thu Feb 6 10:06:20 CET 2003 

When the backslash form of the old style is used, the backslash retains its literal meaning, unless "$", "` "or" \ "follows. The first return lines not preceding the backslash complete the command replacement. When using the $(COMMAND) form, all characters between parentheses make up the command; no one is considered specifically.

Command substitutions can be nested. To nest when using the inverse structure, avoid internal backbeats with backslashes.

If the wildcard appears in double quotation marks, word breaks and file name extensions are not performed based on the results.

+2
source

These are alternative command syntaxes. as @Steve mentions they have different quotation rules, and backticks are harder to embed. On the other hand, they are more portable with the old version of bash and other shells like csh.

+1
source

All Articles