Search code examples
bashexponential

Bash exponentiation limit


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

Solution

  • 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