Shell Arithmetic's with expr and back quotes

Earlier we considered shell arithmetic.


but unfortunately some older shells don't support this. We need an alternative for doing arithmetic and this is the expression command - expr.

If we had a variable i set to a value of zero (0):


We want to add 1 to the value of i, we could say:

i=$(expr i+1)

expr - an external function - will add 1 to the value of i and assign the new value.

Notice that I'm using single round brackets, not double round brackets, primarily because we are running the external command, expr, in the same way that backticks would do.

We could have used the following command to achieve the same result:

i=`expr i+1`

If you don't have double round brackets because you're using an older shell, (which no Linux system will use, but perhaps you are running this course on Solaris or HP/UX where they use the korn shell), then this is the arithmetic construct to use.