exit with error message in bash (oneline)

branquito picture branquito · Jul 6, 2014 · Viewed 55.8k times · Source

Is it possible to exit on error, with a message, without using if statements?

[[ $TRESHOLD =~ ^[0-9]+$ ]] || exit ERRCODE "Threshold must be an integer value!"

Of course the right side of || won't work, just to give you better idea of what I am trying to accomplish.

Actually, I don't even mind with which ERR code it's gonna exit, just to show the message.

EDIT

I know this will work, but how to suppress numeric arg required showing after my custom message?

[[ $TRESHOLD =~ ^[0-9]+$ ]] || exit "Threshold must be an integer value!"

Answer

P.P picture P.P · Jul 6, 2014

exit doesn't take more than one argument. To print any message like you want, you can use echo and then exit.

    [[ $TRESHOLD =~ ^[0-9]+$ ]] || \
     { echo "Threshold must be an integer value!"; exit $ERRCODE; }