Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ${##} mean in bash?

Tags:

bash

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#
like image 440
Holmes.Sherlock Avatar asked Aug 29 '19 05:08

Holmes.Sherlock


People also ask

What does ${ mean in JavaScript?

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

How do you use a backtick?

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.

What is ${} in HTML?

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.

What is ${} syntax?

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"


1 Answers

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

like image 176
oguz ismail Avatar answered Oct 04 '22 02:10

oguz ismail