I know $#
is the number of positional parameters in bash
. But how does bash
interpret ${##}
? Here, is a sample output from my system.
$ echo $#
0
$ echo ${#}
0
$ echo ${##}
1
$ echo $##
0#
${} is just a lot cleaner to insert variable in a string then using + : let x = 5; console. log("hello world " + x + " times"); console. log(`hello world ${x} times`); for ${} to work, the string needs to be enclosed in backticks.
The backtick ` is a typographical mark used mainly in computing. It is also known as backquote, grave, or grave accent. The character was designed for typewriters to add a grave accent to a (lower-case) base letter, by overtyping it atop that letter.
The ${} syntax allows us to put an expression in it and it will produce the value, which in our case above is just a variable that holds a string! There is something to note here: if you wanted to add in values, like above, you do not need to use a Template Literal for the name variable.
So ${} has two different purposes. One of them is variable declaration, as it enables you to enclose any special characters in your variable name: ${special var with spaces} But also allows you to use it in a larger string like: $Var="middle" Write-Host "Before${Var}After"
$#
is the number of positional parameters, and ${##}
is the length of $#
's value in characters; in other words it's the base 10 logarithm plus one, rounded down, of the number of positional parameters. $##
doesn't work because it doesn't comply with the parameter expansion syntax.
Observe:
$ bash -c 'echo "$# ${##}"' _ {1..9}
9 1
$ bash -c 'echo "$# ${##}"' _ {1..10}
10 2
$ bash -c 'echo "$# ${##}"' _ {1..100}
100 3
See Bash Reference Manual § 3.5.3 Shell Parameter Expansion for further information.
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