You're looking for
set -e. From POSIX:
When this option is on, if a simple command fails for any of the reasons listed in Consequences of Shell Errors or returns an exit status value >0, and is not part of the compound list following a while, until, or if keyword, and is not a part of an AND or OR list, and is not a pipeline preceded by the ! reserved word, then the shell shall immediately exit.
In other words, plain commands cause the shell to exit if they fail. (You can use something like
command || true to allow
command to return nonzero.) If you need to perform some cleanup, you can set a trap for the
And better double-quote all your variable expansions. That way your script won't fail horribly if you ever point it at a directory or an URL containing
* or a space.
wget --no-check-certificate "$location" --output-document="$renamed"
mkdir -p "$dir"
echo "extracting to $dir"
tar xf "$file.tar" --directory "$dir" --strip-components 1
echo "Cleaning up..."
rm -r "$file.tar"
Another useful shell idiom to pass optional arguments to a shell script without hassle is to set variables only if they're unset. That way you can pass arguments through the environment, e.g.