Recently I started with writing some shell. I want to increment a local var $COUNTER but I am not sure which and why should I use.

COUNTER=`expr $COUNTER + 1`

COUNTER=$((COUNTER+1))

The first one some how works only sometimes, the second works always. Why is that? Why do I need double parenthesis in the second?

link|improve this question
feedback

1 Answer

There are many options for achiving what you want:

COUNTER=`expr $COUNTER + 1`

COUNTER=$((COUNTER+1))

COUNTER =$[COUNTER+1]

COUNTER =$[$COUNTER+1]

let COUNTER = COUNTER +1

let COUNTER++

All commands should do the same thing such is incrementing COUNTER by 1

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.