View Single Post
  #2   (View Single Post)  
Old 29th September 2008
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,546
Default

() executes a subshell
$FOO = get replace $FOO with value of FOO, so
$() executes a subshell and replaces $() with value of subshell

The `$' character is used to introduce parameter expansion, command substitution, or arithmetic evaluation.

Thus since (cmds) is like doing sh -c "cmds", $(cmds) has to substitute the value of cmds, hence $(cmds) evaluates to the output of 'cmds'. If $ triggers expansion/substitution, that makes a lot of sense right? To my understanding, in the old days: the only way to do simple arithmetic like was with expr. Then later $((expression)) was added to the language for convenience, and people could stop cursing if they missed a space (e.g. expr 2 +2) and had to retype the entire line again.



I can tell you this, shells/v7sh does not allow the $((expression)) syntax that modern /bin/sh's do. Maybe someone like DrJ could explain the history $(()) and expr, I've only used BSD for ~3 years... So I can't really say a lot that is concrete!
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.

Last edited by TerryP; 29th September 2008 at 06:56 AM. Reason: minor typo fix
Reply With Quote