3

I want to understand the reason maximum value for $[a**b] operation in Bash.

$ echo $[2**62]
4611686018427387904
$ echo $[2**63]
-9223372036854775808
$ echo $[2**64]
0
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 2
    Side note: `$[...]` is super old and deprecated, you should use `$((...))` instead. The Bash manual doesn't even mention `$[...]` any longer. – Benjamin W. Dec 10 '19 at 17:48

1 Answers1

5

According to Shell Arithmetic in the Bash manual:

Evaluation is done in fixed-width integers with no check for overflow

So, on a 64-bit platform, for a signed integer, we wrap around at 263:

$ echo $((2**63-1))
9223372036854775807
$ echo $((2**63))
-9223372036854775808
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116