Adding Two Number With Expression Function

ai,

i have one question about shell script regarding for "expr" function

i have using this command "previous=`expr $previous + 1`" where previous value is 0001, but when the script running, the result appear as

expr 1 + 1 = 2 not 0002. The result i need as expr 0001 + 0001 should be 0002...

how to i get the 0002 result...any trick or changes using expr function?

$ 
$ x=0001
$ expr $x + 1 | awk '{printf "%04d\n", $0}'
0002
$ 
$ # or
$ expr $x + 1 | xargs printf "%04d\n"
0002
$ 

tyler_durden

expr is not a funtion. It is an external command for which there is no use in a POSIX shell.

0001 is not an integer; at best it is an octal number.

First, do not use expr, use the shell's arithemetic:

echo $(( 0001 + 0001 ))

I you need it to be padded with zeroes, use printf:

printf "%04d\n" $(( 0001 + 0001 ))