Languages
[Edit]
EN

Bash - break script when some command inside returns error

4 points
Created by:
Nataniel-Barclay
319

In this short article, we would like to show some concept that lets you break Bash script when some command inside returns error.

Assumption: in the below example as error, we understand status different than 0 returned from some program/command.

Practical example:

#!/bin/bash

command_1 || exit 1
command_2 || exit 1

if ! command_3
then
    echo "Some error detected!";
    exit 1
fi

exit 0

Hint: for commands errors you can return different numbers than 1.

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join