View Single Post
  #4   (View Single Post)  
Old 29th September 2008
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Default

$(command) does command substitution

$((expression)) does arithmetic expansion

please see the sh(1) man page.

i=5 ; j=$($i + 1) is not syntactically wrong. here the parameter expansion is done for the token $i then the command 5 is run with the arguments + and 1 and its standard o/p is assigned to $j. (obviously, there is no command by the name 5 on the system.)

i=5 ; j=$(($i + 1)) is what you want. arithmetic evaluation of the expression inside $((...)) is performed and the result 6 is assigned to j.

> how the nested set of parentheses are parsed?

observe the o/p of the following commands:

echo $((ls -l))

echo $((ls -l /)) # if you can tell why the error occurs then you have understood it :-)

echo $( (ls -l /)) # note the space used to seperate the tokens $( and (

Last edited by ephemera; 29th September 2008 at 05:07 PM.
Reply With Quote