Increment variable value by 1 (shell programming) Increment variable value by 1 (shell programming) shell shell

Increment variable value by 1 (shell programming)


You can use an arithmetic expansion like so:

i=$((i+1))

or declare i as an integer variable and use the += operator for incrementing its value.

declare -i i=0i+=1

or use the (( construct.

((i++))


There are more than one way to increment a variable in bash, but what you tried is not correct.

You can use for example arithmetic expansion:

i=$((i+1))

or only:

((i=i+1))

or:

((i+=1))

or even:

((i++))

Or you can use let:

let "i=i+1"

or only:

let "i+=1"

or even:

let "i++"

See also: http://tldp.org/LDP/abs/html/dblparens.html.


The way to use expr:

i=0i=`expr $i + 1`

the way to use i++

((i++)); echo $i;

Tested in gnu bash