← Retour au blog
tech 25 July 2026

The Mysterious Shell Colon: Why Use It?

The shell colon seems useless, but it offers powerful, little-known tricks. Discover how it can simplify your scripts.

Article inspired by the original source
A shell colon does nothing. Use it anyway ↗ refp.se

Introduction: The Shell Colon

In the world of shell scripting, there are countless small tricks that can make our scripts more efficient and elegant. Among these, one stands out for its apparent simplicity: the colon :. At first glance, this symbol seems to do nothing. Yet, it has hidden functionalities that can transform how you write and optimize your scripts. Let's dive into the world of the shell colon and discover why you should use it.

A Bit of History

The colon is a built-in command that dates back to the early days of Unix. In the 1971 Thompson shell, it served as both a label and a comment marker. Today, it is primarily known as the null command, meaning a command that evaluates its arguments without producing output. This might seem useless, but this feature is actually very powerful.

Checking Required Arguments

One of the most practical uses of the colon is its ability to simplify argument checking in a script. Consider a classic scenario where a script requires a mandatory argument:

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

With the colon, this script can be simplified as follows:

``bash : "${1:?missing argument, aborting!}" echo "Hello $1!" ``

Here, the ${1:?} syntax checks if $1 is empty or undefined. If so, the error message is displayed and the script exits with a non-zero status.

Parameter Expansion

The colon can also be used for parameter expansion with default values. For example:

``bash : "${DATA_DIR:=/var/data}" ``

This line assigns /var/data to DATA_DIR if this variable is not already defined.

File Manipulation

For directly manipulating files, the colon is very useful. For instance, to truncate a file without deleting it:

``bash : > error.log ``

This clears the contents of error.log without removing the file itself.

Traps and Signal Handling

When you want to set a trap without executing a command, the colon is your solution:

``bash trap : INT ``

This line sets up a trap for the interrupt signal (INT) with no additional action.

Conclusion

Though it seems to do nothing, the shell colon is a powerful and versatile tool. By using it wisely, you can significantly simplify and optimize your scripts. Whether for argument checking, parameter expansion, or file handling, it offers elegant and efficient solutions.

Let's discuss your project in 15 minutes.

shell colon scripting Unix automation
Deepthix newsletter · 100% AI · every Monday 8am

An AI agent reads tech for you.

Our AI agent scans ~200 sources per week and ships the best articles to your inbox Monday 8am. Free. One click to unsubscribe.

Visit the newsletter page →

Want to automate your operations?

Let's talk about your project in 15 minutes.

Book a call