DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1   (View Single Post)  
Old 29th September 2008
spiderpig spiderpig is offline
Port Guard
 
Join Date: May 2008
Posts: 29
Default incrementing within a shell script?

I know that the following:
Code:
#!/bin/sh
i=0
i=$(($i + 1))
echo $i
is the same as the following:
Code:
#!/bin/sh
i=0
i=$(expr $i + 1)
echo $i
My understanding is that a new shell is spawned to execute ($i + 1) or expr $i + 1, but why are the parentheses needed in ($i + 1)? The best answer I can figure is that the statement:
Code:
i=$($i + 1)
is syntactically wrong because somehow $i in the spawned shell is being interpreted as the name of some application which is to be run. Adding the inner set of parentheses somehow forces the shell to treat the argument ($i + 1) as an expression to be evaluated. I had convinced myself that this was a plausible answer for quite some time until I tried it manually at a shell prompt:
Code:
$ i=1
$ echo $i
1
$ sh ($i + 1)
sh: 1: No such file or directory
So why are the inner set of parentheses required in x=$(($i + 1))?

Thanks!
Reply With Quote
 


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
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


All times are GMT. The time now is 09:37 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick