Home > database >  What does the #*$ in a shell script's string interpolation do?
What does the #*$ in a shell script's string interpolation do?

Time:03-17

I found the following code in a shell script, but I am unsure of what the test condition is evaluating for

if test "${SOME_VAR#*$from asdf/qwer}" != "$SOME_VAR"; then
  echo "##zxcv[message text='some text.' status='NORMAL']";
fi

CodePudding user response:

The combination #*$ does not mean anything special. There are three special symbols and each of them has its own meaning in this context.

${SOME_VAR#abc} is a parameter expansion. Its value is the value of $SOME_VAR with the shortest prefix that matches abc removed.

In your example, abc is *${from} asdf/qwer. That means anything * followed by the value of variable $from (which is dynamic and replaced when the expression is evaluated), followed by a space and followed by asdf/qwer.

All in all, if the value of $SOME_VAR starts with a string that ends in ${from} asdf/qwer then everything before and including asdf/qwer is removed and the resulting value is passed as the first argument to test.

Type man bash in your terminal to read the documentation of bash or read it online.

  • Related