I came across string length manipulation in bash 4.1.2 for two strings
First: 1000000000000000000
developer@kernel ~> echo ${#1000000000000000000}
0
developer@kernel ~> s1=1000000000000000000
developer@kernel ~> echo ${#s1}
19
Second: 10000000000000000000
developer@kernel ~> echo ${#10000000000000000000}
19
developer@kernel ~> s2=10000000000000000000
developer@kernel ~> echo ${#s2}
20
How to explain the strange those behaviors?
${#var}
is normally the length in characters of ${var}
. So ${#1000000000000000000}
is the length of the 1000000000000000000th argument to the script. Since your script wasn't called with that many arguments, this is zero.
Apparently there's a limit on the number of positional parameters, and when you try to refer to a parameter beyond that, the results are unpredictable, and varies from system to system. The dividing line appears to be ${#9223372036854775808}
, which is 264.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With