Mon Apr 27 2026, 19:34:31 +0200

Bash NOP

When I was first learning Bash, I would often write code that looked like this to set environment variables:

if [ -z "$ISHITANI_WOODWORKING" ]; then
  ISHITANI_WOODWORKING="https://www.youtube.com/watch?v=sL96mw1uCmA"
fi

The issue I would often encounter was determining how I should set up my scripts to accept environment variables from the caller. Do I use command line arguments? But then I have to parse them. Should I hard-code them? But then I’d have to remember to do it every time I ran the script.

I’ve dealt with the professional cruelty of so-called engineers and analyst hard-coding parameters inside entire paragraphs of code that you had to manually update every time you had to run their scripts … in production.

There are milder forms of torture that I’m okay inflict upon myself in my own coding practice.

Bash is deceptive. As with any domain, knowing a little about it is much worse than not knowing anything at all. Being familiar with the basics will let you write code like the one above. Yet, Bash has a lithany of operators, gotchas, and footguns (I love that term) that I found vexing at first, but that I learned to understand and appreciate over time.

As an example, Bash has a null command (no-op). A null command always evaluates to true.

I was reminded of it while reading the source code for entr and rset.

One use case for the null command is assigning variables. Instead of writing an if statement, you can

: ${INTERESTING_URL:=https://ncase.me/trust/}

echo "$INTERESTING_URL"

: echo "Hello World"

In this case, if INTERESTING_URL is already set nothing changes. If it’s unset or empty, it gets assigned the URL. The : command just evaluates the expression and discards the result.

The last command will not give any output. That’s because the null command ignores its input, does nothing, always succeeds, and lets the assignment side-effect happen.

If you use Shellcheck, that snippet will trigger SC2223 and urge you to quote the expression. Do with that what you will.

If you’re deluded enough to write code to solve a problem before you have a solution in mind—I’ve been that person—here’s a tip that’ll save you lots of # typing.

You can use the null command in conjunction with here documents. Here’s what it looks like:

echo "This code will execute"

: <<'BLOCK'
echo "This will not execute"
echo "Neither will this"
BLOCK

echo "This code will execute"

The null command is a neat trick to comment out blocks of code in Bash without littering your code with hashtag characters. Everything between <<'BLOCK' and BLOCK is treated as input. Because 'BLOCK' is quoted, no variables or commands are expanded within the here document, which prevents accidental code execution. We’re sending a block of text to a command that ignores everything.

The pattern below shows how to use : to check that certain strings are set,

: ${HOSTNAME?} ${USER?}

We can also truncate files or create them if they don’t exist

: > filename.dat

echo "Hello" >> filename.dat

: >> filename.dat

Next time you want to create an empty function you could do something like this

# function will return true / do nothing

run_func() {
    :
}

References

_will