Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the story behind ${#} in bash

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?

like image 322
http8086 Avatar asked Feb 11 '23 07:02

http8086


1 Answers

${#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.

like image 189
Barmar Avatar answered Feb 13 '23 23:02

Barmar