13

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 2

13

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
5

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

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.