sysxplore

sysxplore

Share this post

sysxplore
sysxplore
Bash 101: The test Command

Bash 101: The test Command

TRÄW🤟's avatar
TRÄW🤟
Jul 17, 2025
∙ Paid
9

Share this post

sysxplore
sysxplore
Bash 101: The test Command
2
Share

If you’ve ever needed your Bash script to make a decision, like checking if a file exists before using it, you’ve probably used the test command, even if you didn’t realize it. It’s a built-in way to evaluate conditions like file existence, string comparisons, and numeric checks. The result is a simple true or false outcome that can steer the flow of your script.

In this guide, you’ll learn how the test command works, the different ways to write conditions in Bash, and when to use each form. We'll also walk through practical examples to show how these checks come in handy during scripting.

📚BONUS

Stay tuned until the end of this guide for something special: a free copy of my Bash Scripting Handbook.

What is the test Command?

The test command in Bash is used to evaluate expressions and return a status code. A status code of 0 indicates that the expression is true, while a non-zero status code indicates false. The test command can be used in three different syntaxes, each with its own advantages:

Using the test Keyword

The most straightforward way to use test is by directly invoking the test command:

test EXPRESSION
FILE=/etc/app/config
if test -f "$FILE"; then
    echo "$FILE exists."
fi

This form is available in all POSIX-compliant shells and is the most portable across different Unix-like systems. It evaluates the given expression and returns a status code indicating whether the expression is true (0) or false (non-zero).

Using Square Brackets [ ]

A more common and readable shorthand for the test command is to use single square brackets:

[ EXPRESSION ]
if [ -f "$FILE" ]; then
    echo "The file exists."
fi

This form is functionally identical to using the test keyword but is often preferred for its simplicity and readability. It’s widely supported and typically used in conditional statements within Bash scripts.

Using Double Square Brackets [[ ]]

Double square brackets [[ ]] are an extended version of the test command available in Bash and some other modern shells like Zsh and Ksh. This syntax provides additional features and more flexibility compared to single square brackets:

[[ EXPRESSION ]]

Key Features of [[ ]]:

  • No Word Splitting or Filename Expansion: Because [[ ]] is built into the shell and doesn’t have legacy requirements, you don’t need to worry about word splitting based on the IFS variable. This means that variables evaluating to strings with spaces won’t be split unexpectedly, so you don’t need to put variables in double quotes as you would with single brackets:

String Comparison: [[ ]] can also handle more advanced string comparison, including lexicographical comparisons:

if [[ "$STRING1" > "$STRING2" ]]; then
    echo "$STRING1 is greater than $STRING2"
fi

Keep reading with a 7-day free trial

Subscribe to sysxplore to keep reading this post and get 7 days of free access to the full post archives.

Already a paid subscriber? Sign in
© 2025 TRÄW🤟
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture

Share