`At first glance, the : (colon) command in Bash looks completely pointless. It's just a single character. It doesn't print anything, doesn't calculate anything, and doesn't seem to do anything useful at all.
So why does it exist?
Surprisingly, the : command is one of the most useful and commonly used "do nothing" commands in Bash scripting. Once you understand how it works, you'll start seeing it everywhere from shell scripts and initialization files to advanced parameter expansion tricks.
In this article, we'll explore what the Bash colon command actually does, why it exists, and several practical examples that every Bash beginner should know.
What Is the : Command?
The : command is known as the null command or the empty command.
Its behavior is very simple:
- It does absolutely nothing.
- It always returns an exit status of
0(success). - The shell still processes everything on the command line normally.
The simplest possible example is:
:
Nothing happens.
If we check its exit status:
:
echo $?
Output:
0
The command succeeded.
Why Does Bash Need a "Do Nothing" Command?
You might wonder why Bash needs a command that literally does nothing.
The answer is surprisingly simple: sometimes Bash syntax requires that something must be present.
Imagine writing a conditional statement where you intentionally don't want anything to happen.
For example:
if some_condition
then
# Do nothing
else
echo "Something happened."
fi
This looks perfectly reasonable to a human, but Bash doesn't agree.
If you write:
if some_condition
then
else
echo "Something happened."
fi
you'll get a syntax error because Bash expects at least one command after the then keyword.
This is where the colon command becomes extremely useful.
Example: Checking Logged-In Users
Let's look at a practical example.
#!/bin/bash
if who | grep "$1" > /dev/null
then
:
else
echo "User $1 is not logged in."
fi
This script:
- Takes a username as its first argument.
- Uses the
whocommand to list logged-in users. - Uses
grepto search for the username. - If the user is logged in, it does nothing.
- Otherwise, it prints a message.
Let's run it.
./check_user.sh pavel
If the user is logged in:
(no output)
If the user isn't logged in:
User pavel is not logged in.
The colon command acts as a placeholder that satisfies Bash's syntax requirements.
What Happens If You Remove It?
Now let's remove the colon.
if who | grep "$1" > /dev/null
then
else
echo "User $1 is not logged in."
fi
If you try running the script, Bash complains immediately:
syntax error near unexpected token 'else'
This happens because Bash expects a command after the then statement. Since nothing is there, the parser becomes unhappy.
Adding:
:
solves the problem.
The Colon Command Isn't Ignored
One thing that often confuses beginners is the assumption that the colon somehow causes the rest of the line to be ignored.
It doesn't.
Everything on the line is still processed normally by Bash.
For example:
: "Hello World"
No output is produced, but Bash still processes the string.
Similarly:
: $(date)
The date command is still executed.
Even variable assignments work:
VARIABLE="AI Server"
: $VARIABLE
The variable is expanded before the colon command executes.
This behavior makes the colon command much more powerful than it first appears.
Parameter Expansion Tricks
One of the most popular uses of the colon command is with parameter expansion.
Suppose we want to set a default value if a variable doesn't exist.
: ${USER_NAME:="Unknown User"}
If:
USER_NAME
doesn't exist, Bash automatically assigns:
Unknown User
to the variable.
For example:
#!/bin/bash
: ${PROJECT_NAME:="My AI Super Server"}
echo $PROJECT_NAME
Output:
My AI Super Server
This trick is used heavily in:
- Docker entrypoint scripts
- Linux installation scripts
- Bash configuration files
- CI/CD pipelines
- DevOps automation
You'll probably encounter it sooner or later if you work with shell scripts professionally.
Using : for Infinite Loops
Another interesting use case is creating infinite loops.
You may have seen something like this:
while :
do
echo "Running..."
sleep 1
done
Since the colon command always returns success (0), the loop condition is always true.
This is effectively equivalent to:
while true
or
for (;;)
Some Bash programmers actually prefer using the colon command because it's built directly into the shell and requires no external command.
Redirecting Output to Files
Because the colon command does nothing, it's often used together with output redirection.
For example:
: > logfile.txt
This command empties the contents of the file without deleting it.
If the file doesn't exist:
logfile.txt
it will be created.
This is a neat trick that's commonly used for:
- resetting log files
- clearing temporary files
- preparing output files
It's shorter and faster than:
echo -n > logfile.txt
Common Beginner Mistakes
Confusing : with Comments
This is NOT a comment:
: This is not a comment
Bash will still process everything after the colon.
If you want a comment, use:
# This is a comment
Thinking It Does Nothing at All
The colon command itself does nothing.
However, Bash still performs:
- variable expansion
- command substitution
- arithmetic expansion
- input/output redirection
- parameter expansion
before executing the command.
So this:
: $(some_expensive_command)
can still take several seconds to execute if the command substitution is expensive.
Practical Use Cases
You'll most commonly see the colon command used for:
| Use Case | Example |
|---|---|
| Empty conditional blocks | then : |
| Infinite loops | while : |
| Default variable values | : ${VAR:=value} |
| Clearing files | : > file.txt |
| Placeholder commands | : |
| Shell scripting tricks | Parameter expansion |
Best Practices
For beginners, the colon command is most useful in three situations:
- When Bash syntax requires a command but you intentionally want to do nothing.
- When assigning default values to variables.
- When creating simple infinite loops or clearing files.
Don't overuse it simply because it looks clever. Readability is always more important than writing cryptic shell scripts.
A good rule of thumb is:
If another developer can immediately understand why you're using the colon command, you're probably using it correctly.
Final Thoughts
The Bash : command may be the simplest command you'll ever learn, but it's far from useless. Despite doing absolutely nothing itself, it plays an important role in shell scripting by satisfying syntax requirements, enabling parameter expansion tricks, simplifying loops, and making file manipulation easier.
Many Bash beginners encounter the colon command in other people's scripts and assume it's some kind of typo or obscure shell magic. In reality, it's simply Bash's elegant way of saying:
"Yes, I know something should happen here—but today, we're intentionally doing nothing."
Sometimes, doing nothing is exactly what your script needs.