Simulation as a method of Embedded System Testing
Simulation as a method of Embedded System Testing
February 28, 2023
priority vs severity
Priority vs Severity
March 14, 2023

March 7, 2023

Stopping on first error in shell scripts

Commands in a shell script are executed from top to bottom. If some commands
fail, the execution continues to the next command. For example:

$ cat my script.sh
#!/bin/bash
echo "Script started"
ls foo
echo "Script ended successfully"
$ ./my_script.sh
Script started
ls: cannot access 'foo': No such file or directory Script ended successfully

Failing commands do not stop the execution of the script. This might be acceptable in some cases but usually, it is wanted to make sure commands are succeeded. Commands can be combined with && or can be checked the result of the previous commands with 'if statements but, this adds extra complexity to the script. There is a better way: 'set -e'

$ cat my script.sh
#!/bin/bash
# set -e # Stop on first error
echo "Script started"
ls foo
echo "Script ended successfully"
$ ./my_script.sh
Script started
ls: cannot access 'foo': No such file or directory
Stopping on first error in shell scripts
This website uses cookies to improve your experience. By using this website you agree to our Data Privacy Statement
Read more