# backtick or $()



## fluca1978 (Oct 21, 2012)

In a Bourne shell script is there any difference (portability, performance) between the execution of an external command using the backticks or using the _$()_ form?


----------



## gordon@ (Oct 21, 2012)

I'm not sure of the performance angle, but I use $() if I need to nest the subcommands. Also, I think $() is a bit more obvious what is happening.


----------



## UNIXgod (Oct 21, 2012)

fluca1978 said:
			
		

> In a Bourne shell script is there any difference (portability, performance) between the execution of an external command using the backticks or using the _$()_ form?



Both are POSIX.

The backticks is Bourne

The other actually was defined in the Korn shell.

The standard put both in as they don't get involved with which is the right one. The benefit is also that you can actually get two layers in all compatible and not just Almquist's shell (i.e. bash/ksh/zsh shells ). I have never actually needed to put backticks inside $( ) but you can.

Further when thinking about portability you may want to consider both the fact that POSIX goes out it's way to not define some of the various extensions specifically to allow some of the OSes to run so called historical commands and others to implement feature bloat on the existing one. For historical ones tar() and cpio() are not defined (though one that didn't exist called pax() is)... Also various UNIXes used various versions of grep. Hence why they defined grep() to incorporate both egrep and fgrep into the same binary.

For an example of non historical extension in the form of feature creep look at the link in my sig.

Your best bet is to use the standard to know what is and is not portable while commenting on your script which dependencies (whether it's an installed program as well as system specific extension)

Here is a link to the standard for your reference: http://rubyprogrammer.net/~stu/posix/

For the performance both should be the same. They both fork another shell after overlaying the memory for it to run the process as gordon mentioned is in fact run as a subprocess.

I recall someone pointing out that the korn syntax $( ) can do arithmetic whereas the bourne will need to use the traditional expr() command.

This may be something that can measure against speed or benchmark. For all practical purposes though ash is really light weight (as with many of the BSD userland commands (in contrast to the GNU counter parts). To some extent you can get to the speed of C in very generic tasks.


----------

