I am trying to debug someone else's script:The code line is:y=$((${oldvalue[$x]}-${newvalue[$x]}))y gets calculated fine as long as both sides are positive numbers.  However, I have a production situation where they are both negative and the error I get is:DEBUG Old value = -4144290000DEBUG New value = -4009685000script.sh: line 123: -4144290000--4009685000: assignment requires lvalueI never would use ksh myself for even the simplest of calculations but I am in a position of production support and have to deal with a big ball of mud, I would use at least Perl/Python. Can anybody tell why this problem is happening and how to fix it?
Error when subtracting two negative numbers in ksh: assignment requires lvalue
shell script;ksh;arithmetic
If you're using the $xxx syntax, then the variable is expanded, and then the result is evaluated as an arithmetic expression.So,y=$((${oldvalue[$x]}-${newvalue[$x]}))Becomesy=$((-4144290000--4009685000))and ksh complains about that unexpected -- operator.You can get around it by adding spaces as you found out, in which case it becomes:y=$((-4144290000 - -4009685000))But, you could also write it:y=$((oldvalue[$x]-newvalue[$x])))In that case, the content of the variable is expanded as part of the arithmetic evaluation and in that case as one numerical value.The different becomes even more important in cases like:a=1+1 b=2echo $((a*b)) vs $(($a*$b))Which gives 4 vs 3, and points to a third and more robust than the first way to work around the problem:y=$(((${oldvalue[$x]}) - (${newvalue[$x]})))