0

In the Bash 215 is not greater then 0330.

$ N=215 ; if [[ $N -ge 0330 ]]; then echo ok ; else echo no; fi
no

But, 216 is not greater than 0330. I think it's so weird. Why is this happening?

$ N=216 ; if [[ $N -ge 0330 ]]; then echo ok ; else echo no; fi
ok
$ N=217 ; if [[ $N -ge 0330 ]]; then echo ok ; else echo no; fi
ok

I expect that '329 -ge 0330' is no and '330 -ge 0330' is ok.

DDBOSS
  • 1
  • 1
  • `0330` (in radix 8) is 3×8×8 + 3×8 = `216` (in radix 10) – ErikMD Mar 31 '23 at 00:09
  • 1
    Bash treats numbers that start with "0" as octal, rather than decimal. See ["Unexpected arithmetic result with zero padded numbers"](https://stackoverflow.com/questions/53205175/unexpected-arithmetic-result-with-zero-padded-numbers). – Gordon Davisson Mar 31 '23 at 00:17
  • Also see [Value too great for base (error token is "08")](https://stackoverflow.com/q/24777597/4154375). – pjh Mar 31 '23 at 00:37
  • `0330 == 216` that's why your `-ge` (greater than or equal to) tests true from `216` on... – David C. Rankin Mar 31 '23 at 02:54

2 Answers2

2

Pick up the radix:

with zero leading, bash 'think' he have to handle octal notation.

$ N=216 ; if ((N < 10#0330)); then echo ok ; else echo no; fi 
#                  ^^^
#                base 10
ok

http://mywiki.wooledge.org/ArithmeticExpression

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
1

0330 is interpreted as an octal number because it starts with a leading zero. In octal notation, the decimal value 330 is represented as 516, which is greater than 215.

Muhammed Jaseem
  • 782
  • 6
  • 18