0

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
feihcsim
  • 1,444
  • 2
  • 17
  • 33
  • 3
    `#*$` does not mean anything special but `${SOME_VAR#abc}` is a [parameter expansion](https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion) that means `$SOME_VAR` with the shortest prefix that match `abc` removed. Type `man bash` in your terminal to read the documentation of `bash` or [read it online](https://www.gnu.org/software/bash/manual/bash.html). – axiac Mar 16 '22 at 17:15

1 Answers1

3

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.

axiac
  • 68,258
  • 9
  • 99
  • 134