Is there a way to make my bash script terminates on the first command that returns non-zero status?
I know I can just chain it with &&
's like:
cd /stuff &&
echo 'what's up' &&
....
Is there another way?
Yes, this is that simple as adding at the beginning of your script after the shebang :
set -e
You can stop this if you want (for just a portion of code) with
set +e
or on the shebang :
#!/bin/bash -e
or by calling the script with :
bash -e script.bash
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With