![]() |
|
Programming C, bash, Python, Perl, PHP, Java, you name it. |
![]() |
|
Thread Tools | Display Modes |
|
|||
![]()
I know that the following:
Code:
#!/bin/sh i=0 i=$(($i + 1)) echo $i Code:
#!/bin/sh i=0 i=$(expr $i + 1) echo $i Code:
i=$($i + 1) Code:
$ i=1 $ echo $i 1 $ sh ($i + 1) sh: 1: No such file or directory ![]() Thanks! |
|
||||
![]()
() 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 |
|
|||
![]() Quote:
|
|
|||
![]()
Thanks, ephemera for answering my question! Where can this information be found? I haven't seen it anywhere.
|
|
||||
![]()
> Where can this information be found?
its all there in the man page. do check it out. also i think we are unnecessarly complicating it. its quite simple really: $(...) and $((...)) do two different jobs and there is no connection between them other than a lexical similiarity. |
![]() |
Thread Tools | |
Display Modes | |
|
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
shell script with zenity | bsdnewbie999 | OpenBSD General | 5 | 24th April 2009 02:37 AM |
shell script-start another process | bsdnewbie999 | Programming | 2 | 23rd April 2009 07:48 PM |
shell script compare md5 sum | bsdnewbie999 | Programming | 1 | 11th April 2009 02:20 PM |
Shell Script. | bsdnewbie999 | Programming | 21 | 15th July 2008 07:54 AM |
shell script with input | c0mrade | Programming | 5 | 13th July 2008 04:33 AM |