How To

Bash Strict Mode: set -euo pipefail Explained

Introduction

Bash strict mode is a useful scripting practice that helps you write safer and more reliable Bash scripts. By default, Bash scripts may continue running even when a command fails, a variable is missing, or a command inside a pipeline returns an error. This can create serious problems in automation scripts, backup scripts, deployment scripts, and cybersecurity scripts.

Strict mode helps detect these problems early. It is commonly written as:

set -euo pipefail

If you are learning Bash scripting, understanding strict mode will help you avoid hidden errors and write cleaner scripts.

What Is Bash Strict Mode?

Bash strict mode is not a separate feature or command. It is a combination of Bash options that change how a script behaves when errors happen.

The common strict mode line is:

set -euo pipefail

This line is usually added near the top of a Bash script, after the shebang.

Example:

#!/bin/bashset -euo pipefailecho "Script started"

What Does set -e Mean?

The set -e option tells Bash to stop the script when a command fails.

Example:

#!/bin/bashset -eecho "Before error"ls /wrong-directoryecho "This line will not run"

If /wrong-directory does not exist, the script stops immediately. This prevents the script from continuing with bad or missing data.

What Does set -u Mean?

The set -u option tells Bash to stop the script when an undefined variable is used.

Example:

#!/bin/bashset -uecho "Username is $username"

If the variable username is not defined, Bash stops the script and shows an error. This is useful because spelling mistakes in variable names can otherwise go unnoticed.

Correct example:

#!/bin/bashset -uusername="kali"echo "Username is $username"

What Does pipefail Mean?

By default, Bash checks only the exit status of the last command in a pipeline. The pipefail option makes the pipeline fail if any command inside it fails.

Example:

#!/bin/bashset -o pipefailcat missing-file.txt | grep "error"

If cat fails because the file does not exist, the pipeline is treated as failed.

Full Bash Strict Mode Example

Create a script:

nano strict-mode.sh

Add this code:

#!/bin/bashset -euo pipefaillog_file="/var/log/auth.log"echo "Checking failed SSH login attempts..."grep "Failed password" "$log_file" | tail -5echo "Check completed"

Run it:

chmod +x strict-mode.sh./strict-mode.sh

This script is safer because it stops if an important error happens.

Conclusion

Bash strict mode helps you write safer scripts by stopping execution when commands fail, undefined variables are used, or pipelines break. The set -euo pipefail line is especially useful for automation, backups, server scripts, and cybersecurity tasks.

For beginners, strict mode is a good habit to learn early. It makes Bash scripts easier to debug and reduces the risk of silent failures.

Cyber Defence

Recent Posts

Install Pip on Ubuntu 18.04: Python 3 and Python 2 Setup Guide

Pip is the official package manager for Python and the standard way to install libraries from…

1 day ago

Install R on Ubuntu 18.04 from CRAN: Statistical Computing Setup

R is an open-source programming language and environment built for statistical computing and data visualization. It…

1 day ago

Install Jenkins on Ubuntu 18.04: CI/CD Server Setup Guide

Jenkins is an open-source automation server that makes it easy to build CI/CD pipelines. Continuous integration…

1 day ago

Install Android Studio on Ubuntu 18.04 with Snap and OpenJDK 8

Android Studio is the official IDE for Android development, built on JetBrains' IntelliJ IDEA platform. It…

1 day ago

Install and Configure GitLab on Ubuntu 18.04 with Omnibus

GitLab is a web-based, open-source Git repository manager written in Ruby. It includes built-in tools for…

1 day ago

Install Anaconda on Ubuntu 18.04: Python Data Science Setup Guide

Anaconda is the most widely used Python distribution for data science and machine learning. It bundles…

2 days ago