Shell – Multiple Command List After Shell Conditionals, &&, ||. Shell Script, Dash

control flowshell

I'm trying to do multiple commands after a condition, so for example…

[ $VAR ] || echo "Error msg" ; echo "exit"

and the Inverse

[ -z $VAR ] &&  echo "Error msg" ; echo "exit"

I know that won't work as intended, I actually knew how previously and forgot how to do this. I'm fully aware of the many alternatives, such as using if's or bracketing via () and {} . Using () will create a sub process which wouldn't exit a running script. Using {} will work, but I know a more readable alternative exists.

I have done this with a :, and it was perfect!!! I just can't remember now for the life of me, and I lost that previously written script.

If anyone knows how to write this with the :'s, I would really appreciate any help!

Best Answer

I tend to use something like this, which I consider nicely readable:

[ -z $VAR ] && {
  echo "Error msg"
  exit ${LINENO}
} >&2

For :, the only thing I can imagine is that you somehow defined a function but I have no idea how that would translate into a block that allows multiple commands to execute.

From man bash:

: [arguments]
        No effect; the command does nothing beyond expanding arguments and
        performing any specified redirections. A zero exit code is returned.

So the only possibility I see is if you had redefined : to be something else. I'll be interested in seeing any possibilities as to what that could be.

Related Question