Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash terminate on first fail

Tags:

bash

shell

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?

like image 324
user1793089 Avatar asked Feb 18 '23 15:02

user1793089


1 Answers

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
like image 174
Gilles Quenot Avatar answered Feb 28 '23 09:02

Gilles Quenot