bash problem?

I am using SunOS 5.9 and have the following outcome when using the sh and bash shells; has anyone else had this:
(tests behave differently)

bash-2.05$ ksh
$ if echo $SHELL; uname -a;[  201010111200 -gt 201010111100 ]; then echo yessssssssssssss; else echo  nooooooooooooooo; fi
SunOS s036cln001014 5.9 Generic_118558-27  sun4u sparc SUNW,Sun-Fire-V890
yessssssssssssss
$ exit
bash-2.05$  bash
bash-2.05$ if echo $SHELL; uname -a;[ 201010111200 -gt 201010111100 ];  then echo yessssssssssssss; else echo nooooooooooooooo; fi
SunOS  s036cln001014 5.9 Generic_118558-27 sun4u sparc  SUNW,Sun-Fire-V890
nooooooooooooooo
bash-2.05$ exit
exit

You are doing numeric comparisons... -gt
201010111200 your number
2147483648 the maximum value for numeric operations that POSIX requires

In other words, you are seeing undefined behavior. ksh happens to work; bash does not -
neither one is required to work. awk,perl,ruby, etc, handle up to 15 digits of precision.

Or change it to a string compare:

[ "201010121200" > "201010121100" ]
1 Like

I believe that introducing the ">" won't solve the problem. Funnily enough, the construct works with bash under SunOS 5.10. I'm merely interested whether anyone else encountered the problem.

ksh and bash-3 installed with Solaris 10 use 64 bit integers. (Note: 9223372036854775807 is 2^63-1)

ksh:

$ typeset -i i=9223372036854775807; echo $i
9223372036854775807
$ typeset -i i=9223372036854775808; echo $i

$

bash-3:

bash-3.00$ typeset -i i=9223372036854775807; echo $i
9223372036854775807
bash-3.00$ typeset -i i=9223372036854775808; echo $i
-9223372036854775808
bash-3.00$ 

The Solaris 9 ksh version seems also to use 64 bit arithmetic.

1 Like

Jim and hergp: I take the point about the 32/64 bit issue (maxima: 2147483648/9223372036854775807); thanks for the information. It's just that my script - using bash - was failing on the 5.9 and working on the 5.10 system).