Home > OS >  decrement the variable value by 1 in shell
decrement the variable value by 1 in shell

Time:01-01

what is the correct syntax to decrement/decrease the variable value by 1 in while loop using /bin/sh and not using /bin/bash script

I used following but does not work

 a=15
 ((a=a-1))   // not working 
 ((a--))       // not working 

EDIT 1

i=0
a=[]
                    b=15
                    while [ $a == [] ] && [ "$i" -le 15 ]
                    do 
                      echo " Waiting ."
                      sleep 60s
                      ((i=i 1))
                      b=`expr $b- 1`
                      a=`some command`
                   
                    done

still getting following error

sh: was: unknown operand /bin/sh: exit: line 186: Illegal number: -1

CodePudding user response:

$ a=15
$ a=`expr $a - 1`
$ echo $a
14

CodePudding user response:

Arithmetic substitution is spelled $(( )) and expands to the result. If you just need the side effect (e.g. increment), use it in a null command:

a=15
: $((--a))
echo $a

Note that shell arithmetic is integer only.

  • Related