From: Chris F.A. Johnson on
On 2005-09-06, Geezer From The Freezer wrote:
> I want to increment a variable without using expr, and using
> just shell builtins
>
> i.e:-
> VAR=1
> VAR=`expr $VAR + 1`
>
> I've seen it done before, but can't remember how its done using
> sh or ksh - any pointers please?

In any POSIX shell (e.g., bash, ksh, and, on many systems, sh):

VAR=$(( $VAR + 1 ))


In a Bourne shell, you can create a string large enough to contain
all the values you will need, and use the positional parameters (so
long as you are using positive integers):

inc()
{
NUMS="1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23"
__VAR=$1
eval "__VAL=\$$__VAR"
set -- $NUMS
shift $__VAL
eval "$__VAR=\$1"
}

Call the function with the name of the variable, not its value:

$ x=11
$ inc x
$ echo $x
12
$ var=7
$ inc var
$ echo $var
8


--
Chris F.A. Johnson <http://cfaj.freeshell.org>
==================================================================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>
From: William Park on
Geezer From The Freezer <Geezer(a)freezer.commy> wrote:
> I want to increment a variable without using expr, and using
> just shell builtins
>
> i.e:-
> VAR=1
> VAR=`expr $VAR + 1`
>
> I've seen it done before, but can't remember how its done using
> sh or ksh - any pointers please?

So many ways. In standard Bash shell,
VAR=$((VAR+1))
((VAR++))
If you have a Bash shell with RPN calculator patched in, then
rpn $VAR x++ VAR=x

--
William Park <opengeometry(a)yahoo.ca>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
http://home.eol.ca/~parkw/thinflash.html
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/
From: Stachu 'Dozzie' K. on
On 06.09.2005, Christophe Gaubert <christophe-gaubert(a)wanadoo.fr> wrote:
> Geezer From The Freezer a ýcrit :
>> I want to increment a variable without using expr, and using
>> just shell builtins
>>
>> i.e:-
>> VAR=1
>> VAR=`expr $VAR + 1`
>>
>> I've seen it done before, but can't remember how its done using
>> sh or ksh - any pointers please?
>
> With bash, I can do :
> (( var= $var + 1 ))
> # or :
> let "var=var+1"
>
> I don't know if it's bash-specific ?

Probably is bash-, ksh- and zsh-specific (doesn't work under ash).
I don't see any such constructions in SUS. Instead, I see
"var=$(( $var + 1 ))".

--
Feel free to correct my English
Stanislaw Klekot
From: Christophe Gaubert on
Stachu 'Dozzie' K. a écrit :
> I don't see any such constructions in SUS.

What is "SUS" ?


--
Christophe Gaubert
http://perso.wanadoo.fr/christophe.gaubert
Mail posté depuis un système libre GNU/Linux
From: John Kelly on
On Tue, 06 Sep 2005 14:14:21 -0400, William Park
<opengeometry(a)yahoo.ca> wrote:

>In standard Bash shell,
> VAR=$((VAR+1))
> ((VAR++))

Since others have shown $VAR inside the double parentheses, it's worth
noting that

VAR=$(($VAR+1))

will work, but

(($VAR++))

will fail. As your example correctly shows, you must use

((VAR++))

without the $ sign. So for consistency, I tend to omit the $ inside
double parentheses.