Need help on ksh

  1. Script using bash
#!/bin/bash
a=4294967296
(( b=$a / 2 ))
echo "$a"
echo "$b"

*** output ***

4294967296
2147483648
  1. Script using ksh
#!/bin/ksh
a=4294967296
(( b=$a / 2 ))
echo "$a"
echo "$b"

*** output ***

4294967296
0

I am using cygwin to run above scripts. It seems to be working on bash but not on ksh.

Any idea why it is not working on ksh?

Your version of ksh on Cygwin is probably pdksh (Public domain ksh.) Check $KSH_VERSION to be certain. pdksh only supports integer arithmetic and uses 32-bit integers for that purpose on Cygwin. Therefore it is limited in the size of the integers which it can handle. As a test, set a to 4000 and see if the result is 2000.

Thanks fpmurphy for the clarification.

The limit in ksh for integer arithmetic is exactly:
((1024 x 1024 x 1024 x 2) - 1)

expr 2147483647 + 0
2147483647

expr 2147483647 + 1
-2147483648

This limit can be circumvented by using "bc".

echo "2147483647 + 1"|bc
2147483648

Once the numbers are above 2147483647 in ksh, store them in character variables because an integer variable will overflow.