Use a check in a chain, without usng if-else.

$ true && echo 'True' || echo 'False'
True

Check if a value is set:

$ [[ -n "$FOO" ]] && echo 'FOO is set'

Warning

When exiting, you probably want to show a message first. Those two operations do not work in the short syntax, as far as I can tell. So you must use an if statement, with echo and exit inside.

These will not work as you might hope:

  • This will always exit, because the exit is evaluated independently after the semicolon.
      $ [[ -z "$MY_VAR" ]] && echo 'MY_VAR must be set' ; exit 1
    
  • And this will never exit, as the exit line here happens within a subshell - the subshell exits with an error status but the outer shell does not exit.
      $ [[ -z "$MY_VAR" ]] && (echo 'MY_VAR must be set' ; exit 1)