Table of Contents
Overflow
It will depend on your version of bash, your OS1) and your CPU2) architecture. To try it, we will set a variable to (2^16). If the output is the right answer, we will set it to (2^32), and so on untill the answer is not right anymore.
In the example below, we can see that at (2^64) the answer is not right anymore. So we substract 1 at the result (2^64) - 1 to know if it is a non-signed INT and we see that the answer is not right yet. So we substract 1 to the exponent (2^63) to know if it is a signed INT and … the answer still wrong.
Then we substract 1 at the result (2^63) - 1 and we see that the result is the answer we were waiting for. We can therefore deduce that it is a signed 64 bit INT. To prove it, we add 1 to the variable and we can see the MIN limit signed INT.
We have now the MIN and the MAX limit.
Example
#!/bin/bash ((X=2**16)); echo $X ((X=2**32)); echo $X ((X=2**64)); echo $X #Overflow ((X=(2**64)-1)); echo $X #Overflow ((X=2**63)); echo $X #Overflow ((X=(2**63)-1)); echo $X #Limit MAX signed int ((X++)); echo $X #Overflow - Limit MIN signed int X=9223372036854775807; echo $X #String or Int, we will never now ((X=($X*1))); echo $X #Int ((X=($X*2))); echo $X #Int overflow ((X++)); echo $X #Int overflow X=9223372036854775908; echo $X #String ((X++)); echo $X #INT overflow
Output will be :
65536 4294967296 0 #Overflow -1 #Overflow -9223372036854775808 #Overflow 9223372036854775807 #Limit MAX signed int -9223372036854775808 #Overflow - Limit MIN signed int 9223372036854775807 #String or Int, we will never now 9223372036854775807 #Int -2 #Int overflow -1 9223372036854775908 #String -9223372036854775707 #INT overflow
Interesting fact :
At the end, we set a variable bigger than the MAX limit and increment X (in this case X=1). The variable is at first stocked as a STR but when we force it to increment by X, we see that bash will compensate and output the MIN limit incremented by X.