Demystifying a Common Shell Script

Image by Susbany on Pixabay

Being around for a while each of us might have seen the following code

In the wild it can be found at the beginning of some shell scripts. It is a guard clause.

Our script requires an input and fails if the length of the input is zero. If the length is zero it’s not present. Thus the -z.

Without the input it would make no sense to continue with the execution of thhe script. As a result we exit with a non-zero status. exit 1.

After a quick search for “bash check if input is provided” we could grab the above straight from Stackoverflow. No further considerations. It’s fine 😉

Let’s have some fun with the shell to dive deeper.

We regard> as the prompt and the line which follows afterwards as the output.

Hah. Seems like [ is a command. No if required.

We check out the manual (manpages)

Typing /-z we search and find the docs of the -z argument stating

-z | string | True if the length of string is zero.

It also states [ is the utility test. In fact we’re able to interchange [ with test.

We can even build an alternative to test in our script. For the sake of improving the readability.

We formulate the most important requirements of our alternative assert-emptyinto tests.

Voila. A home grown test framework which consists of a single function.

The && failed leads to a failure if the command before exits successfully. Which it should not.

And || failed triggers if the command before exits unsuccessfully. Which again, it should not.

Let’s go for a quick and dirty implementation in C. If we have no input or if the input has a length of zero we exit successfully.

We compile the above

And run the tests


Copy the compiler output to /usr/local/bin to reuse it

The script above could now start with

We should not use it in our scripts though. Their portability would be destroyed. Only our system will have the assert-empty utility.

Having a look at the original source of test in C we find it offers a lot more than our assert-empty. It had time to mature. The commit Initial revision of the file dates back to November 1992.

Spelunking around the tests folder of the repository we find how test is used to test other coreutils, such as rm.

Check out some cool parts in one of the files used to test rm

We recognise the classic Arrange-Act-Assert.

  • Arrange: We create a directory.
  • Act: We delete the directory
  • Assert: We verify whether it still exists with test

Thus what seemed like an innocent bracket [ helps to make sure the foundations we build upon run smoothly.

Building things. Usually by writing code. Software Engineering @porschedigital

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store