Three years ago, I wrote Take your BASH scripting seriously; while this article stands on its own, I do recommend reading that first instalment.
In this one, we'll cover two important topics:
- Costly (and, at times, dangerous) misuse of commands
- Useful BASH options everyone should understand and use (wisely!)
For an interactive walk-through of the topics discussed in this article, play the trecs recording (you can copy the examples directly off the player and navigate the commands and chapters like a boss):
Take your BASH scripting seriously - trecs
Costly Anti-patterns
Anti-patterns exist in every programming language and BASH is no exception. But in fact, the ones we'll be covering in this section only pertain to BASH vicariously; because really, the shell (BASH or any other) in these cases is just the glue (or wrapper if you will) and the actual problem is people not taking the time to properly learn the commands' usage and defaulting to> /dev/null, or, worse yet - to > /dev/null 2>&1 far too often; much like a bad housekeeper (and I should know, I am one), would use a broom to push the dirt under the carpet. LLMs, of course, produce the very same anti-patterns just as frequently, because LLMs are trained on human written materials.
Let's start with a simple, and relatively harmless one:
cat /some/file | grep "some string"
grep stars in several of these examples, not because there's anything wrong with the utility (it is amazing!), but because, again, people do not take the time to properly familiarise themselves with it. In the above, the first part is completely redundant, because grep knows how to operate directly on files (not just STDIN). I say this one is relatively harmless because cat is a rather quick operation and if you use time to measure the difference between cat /some/file | grep "some string" and grep "some string" /some/file you'll find it's typically negligible.
Having said that, for completeness' sake, let's consider an example where it could be more expensive (in particular on large files), unintentionally. Imagine a machine where cat is an alias (aliases do not expand by default, but can be made to) or a symlink to batcat (https://github.com/sharkdp/bat). Now try running it with time.
This benign example, perhaps especially because it is so trivial, illustrates the underlying principle well: if you do not need it, leave it out!
Let's move on to a far more dangerous example, also involving grep:
MY_FILE="/path/to/some.log"
if grep "some string" MY_FILE > /dev/null 2>&1;then
echo "Found, let's do something here"
else
echo "Not found, let's do something else"
fi
If you've taken a moment to properly read this, you may have spotted an obvious error in the second line: grep should be given $MY_FILE as a parameter, not MY_FILE.This is quite intentional (but also, a mistake programmers can, and in fact - do, make - the above is something I've encountered when conducting a code review).
Running this code will, of course, always output:
Not found, let's do something else
and the error may go unnoticed for some time. Here's the revised version:
MY_FILE="/tmp/some.log"
if grep -q "some string" MY_FILE ;then
echo "Found, let's do something here"
else
echo "Not found, let's do something else"
fi
From the man page:
-q, --quiet, --silent
Quiet; do not write anything to standard output.
Exit immediately with zero status if any match is found,
even if an error was detected.
With this, you'll get:
grep: MY_FILE: No such file or directory
Not found, let's do something else
Ah! Much better, harder to miss that one when testing or reviewing logs.
Three other common mistakes relating to grep before we move on:
- When you know the pattern's casing, do not use -i, not only can it lead to unexpected, false matches, it's also more expensive
- If you just want to know the match count, use -c, do not pipe to wc -l (a variation on the cat story)
- When you want to find complete words, use -w, that way, with the above example, some stringaaa will not be considered a match
Okay, let's look at a really amusing one (again, encountered during a real code review):
tar zxvf /some/archive.tar.gz > /dev/null 2>&1
From the man page:
-v, --verbose
Verbosely list files processed.
Each instance of this option on the command line increases the verbosity
level by one.
In other words, the above is akin to asking someone to tell you how they did something and then hitting the mute button while they do so; if you don't want to know, don't ask:)
Another common one:
rm /some/file > /dev/null 2>&1
From the man page:
-f, --force
ignore nonexistent files and arguments, never prompt
rm -f /some/file
Cleaner, shorter and, if there are other errors rm encountered, you will see them.
Let's close this section with a fun one. This one, I'm almost positive was generated by an LLM (once more - a real example, from a code review I conducted, part of a CVE reproduction):
REPL=$(python3 -c "import sys; sys.stdout.write('z'*256)")
Run this with time and then compare that to:
REPL=$(head -c 256 /dev/zero | tr '\0' 'z')
Or, faster still:
printf -v REPL 'z%.0s' {1..256}
I say I'm almost positive it was generated by an LLM because LLMs are notorious for falling back on Python, even when there's no justifiable reason to introduce it into the code. I suspect it's because they were trained on Python code more than on anything else (which is a shame).
Useful options
Let's start this section with -e. Consider this snippet:
#!/bin/bash
SOME_DIR="/path/to/dir"
if [ ! -d "$SOME_DIR" ];then
echo "$SOME_DIR does not exist, exiting"
exit 1
fi
cd $SOME_DIR
if ! gcc somefile.c -o somefile ;then
echo "Compilation failed"
exit 1
fi
Does this code work? yes. But it can read much better while also being far shorter. Enter -e.
From man bash-builtins, under the set section:
-e Exit immediately if a pipeline (which may consist of a single
simple command), a list, or a compound command (see SHELL GRAMMAR in bash(1)),
exits with a non-zero status.
So, now, let's revise our snippet accordingly:
#!/bin/bash
set -e
SOME_DIR="/path/to/dir"
cd "$SOME_DIR"
gcc somefile.c -o somefile
To save you the counting, that's 4 lines instead of 10, doing the same job while being stricter, more succinct and more readable.
Of course, when you want to allow a command to fail, you can use set +e before invoking it and then restore set -e once it's done.
Another handy one is -u.
Once more, from the bash-builtins man page:
-u Treat unset variables and parameters other than the special parameters
"@" and "*", or array variables subscripted with "@" or "*", as an error when
performing parameter expansion. If expansion is attempted on an unset variable
or parameter, the shell prints an error message, and, if not interactive,
exits with a non-zero status.
Let's look at an example:
#!/bin/bash
set -eu
. /etc/os-release
echo "My version is: $DEBIAN_VERSION"
There's an intentional (and a very human mistake) above, /etc/os-release looks like this (on my Debian 13):
PRETTY_NAME="Debian GNU/Linux 13 (trixie)"
NAME="Debian GNU/Linux"
VERSION_ID="13"
VERSION="13 (trixie)"
VERSION_CODENAME=trixie
DEBIAN_VERSION_FULL=13.6
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
So, as you can see, the var we're aiming for is called DEBIAN_VERSION_FULL, not DEBIAN_VERSION.
The above snippet, thanks to -u, will output:
/tmp/minus-u.sh: line 6: DEBIAN_VERSION: unbound variable
And exit with RC 1, making it very clear that you have an issue, long before you ship it to your colleagues, or, worse yet - your customers.
Without -u, you'll simply get:
My version is:
And the execution will continue, and depending on your code, may cause damage or, at best, only confusion.
Next, we have -o pipefail:
If set, the return value of a pipeline is the value of the last (rightmost)
command to exit with a non-zero status, or zero if all commands in
the pipeline exit successfully. This option is disabled by default.
Pipes are one of the most brilliant UNIX inventions of all time and we all use them constantly. To illustrate the importance of pipefail, let's use this trivial example:
#!/bin/bash
set -e
lsb_releas -a | grep Description | awk -F ':[[:space:]]' '{print $NF}'
uname -a
Here I've intentionally introduced a typo, because I do have lsb_release installed on my machine, but bear in mind that for many distros, it is NOT part of the base install. If you run the above, you'll get:
/tmp/pipefail: line 5: lsb_releas: command not found
Linux port 6.12.100+deb13-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.12.100-1 (2026-07-30) x86_64 GNU/Linux
In other words, after the failure to invoke lsb_releas, execution continues, despite having set -e. But if we change the above to set -eo pipefail, we'll get the desired result:
/tmp/pipefail: line 5: lsb_releas: command not found
And the script will exit with RC 1, without ever running uname.
At this point, you may be thinking, "well, okay, but what if we do run the grep and awk here? Other than the ugly error, no harm done, right?"...
But consider a script that includes more destructive commands down the line, that depend on the result of this one, like rm "/etc/$SOME_VAR", well, that wouldn't be as benign, now would it?
One note about the above example: if you've been following closely, you'll surely spot it demonstrates yet another faulty pattern, because, to get the distro's description, we can simply run lsb_release -ds and save the grep, or, better yet:
. /etc/os-release # this file is part of the `base-files` package
echo "$PRETTY_NAME"
Which does not require having lsb_release installed at all. I chose this example specifically to make a double point.
Before we move on to my very favourite option - -x - two salient points about the ones we've already covered:
- You can set -e in the shebang itself, thusly: #!/bin/bash -e. And that feels very elegant but, bear in mind that people sometimes invoke bash /your/script for various reasons (for example, if they want to use the very -x I am about to cover), and in that case, it will not be set for them, so an explicit set -e is better
- POSIX.1-2024 introduced pipefail, so if you use a different, older shell, make sure it supports it (dash 0.5.12-12 and zsh 5.9-8+b24, tested on my Debian 13, both do but beware!). In part I of this series, I spoke of ShellCheck and if you're not yet familiar with it, I recommend that you check it out
Okay, now let's discuss -x:
-x After expanding each simple command, for command, case command, select
command, or arithmetic for command, display the expanded value of PS4, followed
by the command and its expanded arguments or associated word list.
The man entry, while accurate, does not make it sound as handy as it actually is:)
For illustration purposes only (normally, you should use service or systemctl if your distro supports systemd), here's a truncated snippet of bash -x /etc/init.d/apache2 status:
+ DESC='Apache httpd web server'
+ NAME=apache2
+ DAEMON=/usr/sbin/apache2
+ SCRIPTNAME=apache2
+ SCRIPTNAME=apache2
+ '[' -n '' ']'
+ '[' apache2 '!=' apache2 ']'
+ DIR_SUFFIX=
+ APACHE_CONFDIR=/etc/apache2
+ '[' -z '' ']'
+ APACHE_ENVVARS=/etc/apache2/envvars
+ export APACHE_CONFDIR APACHE_ENVVARS
+ ENV='env -i LANG=C PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
+ '[' /etc/apache2 '!=' /etc/apache2 ']'
+ '[' /etc/apache2/envvars '!=' /etc/apache2/envvars ']'
++ . /etc/apache2/envvars
+++ unset HOME
+++ '[' /etc/apache2 '!=' /etc/apache2 ']'
+++ SUFFIX=
+++ export APACHE_RUN_USER=www-data
+++ APACHE_RUN_USER=www-data
+++ export APACHE_RUN_GROUP=www-data
+++ APACHE_RUN_GROUP=www-data
+++ export APACHE_PID_FILE=/var/run/apache2/apache2.pid
+++ APACHE_PID_FILE=/var/run/apache2/apache2.pid
+++ export APACHE_RUN_DIR=/var/run/apache2
+++ APACHE_RUN_DIR=/var/run/apache2
+++ export APACHE_LOCK_DIR=/var/lock/apache2
+++ APACHE_LOCK_DIR=/var/lock/apache2
+++ export APACHE_LOG_DIR=/var/log/apache2
+++ APACHE_LOG_DIR=/var/log/apache2
+++ export LANG=C
+++ LANG=C
+++ export LANG
++ echo /var/run/apache2/apache2.pid
+ PIDFILE=/var/run/apache2/apache2.pid
+ VERBOSE=no
+ '[' -f /etc/default/rcS ']'
+ . /lib/lsb/init-functions
+++ run-parts --lsbsysinit --list /lib/lsb/init-functions.d
++ for hook in $(run-parts --lsbsysinit --list /lib/lsb/init-functions.d 2> /dev/null)
++ '[' -r /lib/lsb/init-functions.d/00-verbose ']'
++ . /lib/lsb/init-functions.d/00-verbose
Try that with your own scripts, tell me it's not brilliant for debugging, I dare you:)
Happy scripting,
Psst..
Got a build, packaging, deployment or infrastructure challenge? Get in touch.
Cheers,
Top comments (0)