DEV Community 👩‍💻👨‍💻

Cover image for Shell settings for safer scripts
Adam K Dean
Adam K Dean

Posted on

Shell settings for safer scripts

I was just reading through one of Progrium's scripts when I came across set -eo pipefail at the beginning of a script. Having not seen that before, I decided to Google. This is the result of that.

You can use set to manipulate shell variables and functions. Some of these can help you write safer scripts.

set -e
Enter fullscreen mode Exit fullscreen mode

If any command fails, set -e will make the entire script fail, rather than just skipping onto the next line. If you want to allow a line to fail then you can pop || true onto the end of it.

set -u
Enter fullscreen mode Exit fullscreen mode

This will treat unset variables as an error, and immediately exit the script.

set -o pipefail
Enter fullscreen mode Exit fullscreen mode

By default only the last command in a list of piped commands returns a failure code if it fails. By using set -o pipefile, if any of the commands fail, the line will fail. Using this with set -e means that if any command in a piped command fails, the script will fail.

Now back to my reading...

Top comments (0)

🌚 Browsing with dark mode makes you a better developer.

It's a scientific fact.