What does a hash after a variable name do as an operator in bash?

I found this code:

EXTENSION="${i#*=}"

What it does is to take the variable $i, which is an argument for the script and copies everything after the character =. So if I do something like myscript.sh -e=wow, it copies wow to $EXTENSION. But I want to know what the symbols #*= mean in this order? It seems like #* is together the copy all after and = is the character after which it copies, or is it more complex?

2 Answers

That is an example of prefix removal. The general form is:

 ${variable#pattern}

which removes the shortest match to the glob pattern from the beginning of variable. In your case, pattern consists of (a) * which matches zero or more of any character, and (b) = which matches just =.

See man bash for more info.

Example

$ i='ab=cd'
$ echo "${i#a}"
b=cd
$ echo "${i#*=}"
cd

Bash Reference Manual

3.5.3 Shell Parameter Expansion

[…]

${parameter#word}

The word is expanded to produce a pattern just as in filename expansion (see Filename Expansion). If the pattern matches the beginning of the expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern […] deleted.

Example

$ i='ab=cd'
$ echo "${i#a}"
b=cd
$ echo "${i#*=}"
cd

Shamelessly stolen from John's answer

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like