NCEP CCS Conversion Guide
Issues Converting to AIX 5.1
Back to Main Page
Topic: Arithmetic Evaluation in Korn Shell with Leading Zeroes
Posted by:  Stephen Gilbert
Updated by: David Michaud
Post Date: 08/21/2002
Last Updated: 08/22/2002
Problem:   If using the newval=$((var1*var2)) syntax in ksh to multiply two integer values, any value with a leading zero ( e.g. typeset -Z2 a; a=8 OR a=08) is interpreted as an octal value.  It is interpreted as a decimal value on asp/bsp. 

For exmple:
$((07*010)) returns 56 on snow/frost
$((07*010)) returns 70 on asp/bsp

$((07*08)) returs a ksh error on snow/frost since 8 is not a valid octal value
$((07*08)) returns 56 on asp/bsp

The comand syntax above is the same as using the let command as in let "newval=var1*var2".

Solution: This change occurs between AIX4 and AIX5 because ksh88 was fixed to conform to POSIX standard.  Therefore, the behavior in AIX5 is expected.  To get around this issue you can do one of two things.  If you are doing string manipulation and want to drop the leading zeroes intentionally, you can do a "typeset -LZ" for the variable you are storing the value in.  Or, you can use the expr command.  However, using expr causes ksh to fork a child process which will be slower than the ksh arithmetic substitution.

Example:

var1=07
var2=08

Change
    newval=$((var1*var2))
to
    newval=$(expr $var1 \* $var2)

or

Add this before your arithmetic substitution
typeset -LZ var1
typeset -LZ var2
 

Back to Main Page