In the realm of terminals, a simple semicolon (:) plays a crucial role that many may underestimate. In situations where the script requires arguments, checking for mandatory arguments is a routine task. Traditionally, this is done with an if-statement, but there is a more elegant way that uses the semicolon to save lines of code:

if [ -z '$1' ]; then
  echo 'missing argument, aborting.' 1>&2
  exit 1
fi
echo 'Hello $1!'

We can rewrite the argument check in just one line using the null command and parameter expansion:

: '${1:?missing argument, aborting.}'
echo 'Hello $1!'

By referencing a variable with an appropriate name, the behavior is the same as before, but easier to identify, as it includes the variable name in the diagnostic:

: '${GREET_NAME:?missing argument, aborting.}'
echo 'Hello $GREET_NAME!'

The parameter expansion ${name:?diagnostic} checks if $name is unset or empty. If so, the diagnostic message is printed to stderr and the shell exits with a non-zero status. If the variable is set, it is equivalent to $name.

The null command (:) is a builtin that does nothing but process its arguments and discard the result. It is so old that it dates back to the Thompson shell of 1971, where it acted as a label and the first comment marker in Unix.

There are several ways to use the null command that may surprise you. It can be used to set defaults, truncate log files, and check file readability and writability, as in the following example:

: '${DATA_DIR:=/var/data}' # set defaults
: '${RETRIES:=3}' # instead of running it as a command
: > error.log # truncate error.log
: > error.log > access.log # truncate both error.log and access.log
( : < dataset.json ) && echo YES # is dataset.json readable?
( : >> result.json ) && echo YES # is result.json writable?

If you prefer to write less and go fast, the null command and parameter expansion are a duo worth studying before your coffee gets cold.

There are questions about the necessity of the null command and how it compares to traditional variable assignment. The null command prevents the parameter expansion from being treated as a command to execute, instead of just evaluating the expression.

By using the null command, we reduce the number of potential typographical errors from two to one, making the code cleaner and less error-prone.

Conclusion: the mighty semicolon in shell scripting is a versatile tool that improves code readability, efficiency, and accuracy, making it a valuable ally for terminal users.