Best practice to use $? in bash?
One common way is:
die() {
IFS=' ' # make sure "$*" is joined with spaces
# output the arguments if any on stderr:
[ "$#" -eq 0 ] || printf '%s\n' "$*" 1>&2
exit 1
}
then you use it like this:
mkdir -p some/path || die "mkdir failed with status $?"
Or if you want it to include the exit status, you could change it to:
die() {
last_exit_status=$?
IFS=' '
printf '%s\n' "FATAL ERROR: $* (status $last_exit_status)" 1>&2
exit 1
}
and then using it is a bit easier:
mkdir -p some/path || die "mkdir failed"
When it fails, mkdir
will likely already have issued an error message, so that second one may be seen as redundant, and you could just do:
mkdir -p some/path || exit # with the same (failing) exit status as mkdir's
mkdir -p some/path || exit 1 # with exit status 1 always
(or use the first variant of die
above without argument)
Just in case you haven't seen command1 || command2
before, it runs command1
, and if command1
fails, it runs command2
.
So you can read it like "make the directory or die".
Your example would look like:
mkdir -p some/path || die "mkdir failed"
cd some/path || die "cd failed"
some_command || die "some_command failed"
Or you can align the dies
further on the right so that the main code is more obvious.
mkdir -p some/path || die "mkdir failed"
cd some/path || die "cd failed"
some_command || die "some_command failed"
Or on the following line when the command lines are long:
mkdir -p some/path ||
die "mkdir failed"
cd some/path ||
die "cd failed"
some_command ||
die "some_command failed"
Also, if you are going to use the name some/path
multiple times, store it in a variable so you don't have to keep typing it, and can easily change it if you need to. And when passing variable arguments to commands, make sure to use the --
option delimiter so that the argument is not taken as an option if it starts with -
.
dir=some/path
mkdir -p -- "$dir" || die "Cannot make $dir"
cd -P -- "$dir" || die "Cannot cd to $dir"
some_command || die "Cannot run some_command"
You could rewrite your code like this:
#!/bin/bash
function try {
"$@"
code=$?
if [ $code -ne 0 ]
then
echo "$1 did not work: exit status $code"
exit 1
fi
}
try mkdir -p some/path
try cd some/path
try run_some_command
If you don't actually need to log the error code, but just whether the command succeeded or not, you can shorten try()
further like so:
function try {
if ! "$@"
then
echo "$1 did not work"
exit 1
fi
}
If you really want to exit
on an error and are using Bash, then you should also consider set -e
. From help set
:
-e Exit immediately if a command exits with a non-zero status.
This of course doesn't give you the flexibility of a did_it_work() function, but it is an easy way to make sure your bash script stops on an error without adding lots of calls to your new function.