Test command in bash

Bash, the powerful scripting language for Linux and Unix systems, offers a versatile tool for checking conditions: the test command (and its convenient alternatives). Let’s explore how test empowers your scripts to make informed decisions and handle errors gracefully.

What is the Test Command?

The test command (or its equivalent square bracket syntax []) evaluates expressions and returns a status code:

  • 0 (true) if the expression is true.
  • 1 (false) if the expression is false.

This simple mechanism opens a world of possibilities for building robust scripts.

Syntax

There are three common ways to use the test command:

# Using the 'test' keyword
test expression

# Using square brackets (preferred for readability)
[ expression ]

# Using double parentheses (for arithmetic expressions)
(( expression ))

Common Test Operators

  • File Tests:
    • -f file: True if file exists and is a regular file.
    • -d file: True if file exists and is a directory.
    • -s file: True if file exists and has a size greater than zero.
    • -r file: True if file exists and is readable.
    • -w file: True if file exists and is writable.
    • -x file: True if file exists and is executable.
  • String Tests:
    • -z string: True if string is empty (zero length).
    • -n string: True if string is not empty.
    • string1 = string2: True if the strings are equal.
    • string1 != string2: True if the strings are not equal.
  • Numeric Tests:
    • num1 -eq num2: True if num1 is equal to num2.
    • num1 -ne num2: True if num1 is not equal to num2.
    • num1 -lt num2: True if num1 is less than num2.
    • num1 -gt num2: True if num1 is greater than num2.
    • num1 -le num2: True if num1 is less than or equal to num2.
    • num1 -ge num2: True if num1 is greater than or equal to num2.
  • Logical Operators:
    • ! expression: Logical NOT (negation)
    • expression1 -a expression2: Logical AND
    • expression1 -o expression2: Logical OR

Examples

Example 1: Checking File Existence and Permissions

file="important_data.txt"

if [ -f "$file" -a -r "$file" ]; then
  echo "File exists and is readable!"
else
  echo "Either the file doesn't exist or is not readable."
fi

Example 2: Comparing Strings

word1="hello"
word2="world"

if [ "$word1" != "$word2" ]; then
  echo "The words are different."
else
  echo "The words are the same."
fi

Output:

The words are different.

Example 3: Arithmetic Comparison

count=15

if (( count > 10 )); then
  echo "Count is greater than 10"
fi

Output:

Count is greater than 10

Why Use Test in Your Scripts?

  • Error Handling: Validate input, check file conditions, and anticipate potential issues before they cause problems.
  • Decision Making: Build smarter scripts that adapt based on different circumstances.
  • Improved Readability: The square bracket syntax is often clearer than a series of nested if statements.

Leave a Reply

Your email address will not be published. Required fields are marked *