1

I am trying to strip the string value in the variable and copy to another variable using bash parameter expansion.

initialModified=${initial#abc}

This give me the value in initialModified after stripping out abc from the beginning of the string in initial variable when I echo initialModified.

However, along with this I would like to add required check

initialModified=${initial#abc:?Required parameter}

This doesn't work as expected. And throws an error. Any help is appreciated.

dreddy
  • 463
  • 1
  • 7
  • 21
  • 1
    Is there a reason you need to accomplish both things on a single line? Otherwise why not `initialModified=${initial#abc}`, followed by `initialModified=${initialModified:?Required parameter}`? – Mike Holt Mar 08 '19 at 21:26
  • Trying to see if I can get both in a single command – dreddy Mar 08 '19 at 21:28
  • I've tried similar things before, and never had any luck with it. I could be wrong, but AFAIK, `bash` does not allow combining multiple substitution rules in a single expansion. – Mike Holt Mar 08 '19 at 21:30
  • @WiktorStribiżew "Required" in this context is a special Bash parameter expansion that exits if the parameter to be expanded is null or unset, `${parameter:?word}` (uses the expansion of `word` as the error message), see [manual](https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion). – Benjamin W. Mar 08 '19 at 21:44
  • 1
    Bash was not designed to do multiple expansions in one command. Just use the multiple commands and be happy that they work. – John1024 Mar 08 '19 at 22:03
  • 1
    To expand on that, you **cannot** do multiple expansions like this in one command. – l0b0 Mar 08 '19 at 22:42

0 Answers0