How to handle numeric variables in sh?

Alejandro Pulver alejandro at varnet.biz
Sat Feb 19 17:05:55 GMT 2005


On Sat, 19 Feb 2005 10:24:04 -0500
Scott Milliken <scott.milliken at e-comsultant.net> wrote:

> Here's a code snippet of a script I use often to number a bunch of pics 
> in a directory that I think shows how to do what you want to do:
> 
> $PICNUM=100
> for FNAME in DSC*.JPG
> do
>    NEWNAME=My_Pictures-$PICNUM.jpg
>    mv $FNAME $NEWNAME
>    PICNUM=$(($PICNUM+1))
> done
> 
> This works in /bin/sh from my FreeBSD 4.11 system and also bash.  If you 
> want to perform a mathematical operation on a shell variable, just 
> surround the expression with $(( expr )) and it'll work.
> 
> Hope that helps.
> 
> Scott Milliken
>

Thank You.

Y saw the use of '$(( expr ))' some time ago in 'Advanced Bash Scripting Guide', but I thought it was only for 'bash'.

I am using FreeBSD 5.3 and it also works in '/bin/sh'.

Best Regards,
Ale


More information about the freebsd-questions mailing list