Mohamed Elashri

Learning set -euo pipefail

I’ve been writing shell scripts since I started learning about computers and programming. This close to two decades by now. They started simple: scp a file here, loop over a few filenames there. Over time, the scripts grew some evolved into wrappers for pipelines, cron jobs, Docker entrypoints, or even local dev automation. I thought I knew what I was doing.

But recently, I stumbled (again) on something I’d seen many times but never fully respected:

set -euo pipefail

I’d copied it before without thinking. Assumed it was “safer” somehow. I never gave it more attention than a passing glance.

Now I actually know what it does. And it’s changed how I write scripts which I do now more than the number of meals I eat in a week.

So what does it do?

This combo enforces strict, predictable behavior. It prevents a large class of silent errors. For example:

grep "match" file.txt | head -n 1

If file.txt doesn’t exist, grep fails. But head still exits 0. So your script looks successful even though it wasn’t. With pipefail, that failure doesn’t get swallowed.

Since adopting it seriously, I’ve caught bad variable names, missing files, broken assumptions, and silently ignored errors before they caused downstream problems.

But it’s not all upside.

Once you enable this, you need to be deliberate:

So why bother?

I wish I learned this when I first started scripting, but I was too lazy, so I didn’t. But I’m using it now. And now, I won’t write another serious script without it.