Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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