test

shell scriptingLinux/Unix
The test command is one of the most frequently used commands in Linux/Unix-like operating systems. test Check file types and compare values

Quick Reference

Command Name:

test

Category:

shell scripting

Platform:

Linux/Unix

Basic Usage:

test [options] [arguments]

Common Use Cases

    Syntax

    test EXPRESSION
    [ EXPRESSION ]
    [ OPTION

    Options

    Option Description
    File Test Operators
    -b FILE True if FILE exists and is a block special file
    -c FILE True if FILE exists and is a character special file
    -d FILE True if FILE exists and is a directory
    -e FILE True if FILE exists
    -f FILE True if FILE exists and is a regular file
    -g FILE True if FILE exists and is set-group-ID
    -h FILE True if FILE exists and is a symbolic link (same as -L)
    -k FILE True if FILE exists and has its sticky bit set
    -L FILE True if FILE exists and is a symbolic link
    -p FILE True if FILE exists and is a named pipe
    -r FILE True if FILE exists and is readable
    -s FILE True if FILE exists and has a size greater than zero
    -S FILE True if FILE exists and is a socket
    -t FD True if file descriptor FD is open and refers to a terminal
    -u FILE True if FILE exists and its set-user-ID bit is set
    -w FILE True if FILE exists and is writable
    -x FILE True if FILE exists and is executable
    -O FILE True if FILE exists and is owned by the effective user ID
    -G FILE True if FILE exists and is owned by the effective group ID
    -N FILE True if FILE exists and has been modified since it was last read
    FILE1 -nt FILE2 True if FILE1 is newer than FILE2 (modification date)
    FILE1 -ot FILE2 True if FILE1 is older than FILE2
    FILE1 -ef FILE2 True if FILE1 and FILE2 refer to the same device and inode numbers
    String Operators
    -z STRING True if STRING is empty
    -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 Comparison Operators
    INT1 -eq INT2 True if INT1 is equal to INT2
    INT1 -ne INT2 True if INT1 is not equal to INT2
    INT1 -gt INT2 True if INT1 is greater than INT2
    INT1 -ge INT2 True if INT1 is greater than or equal to INT2
    INT1 -lt INT2 True if INT1 is less than INT2
    INT1 -le INT2 True if INT1 is less than or equal to INT2
    Logical Operators
    ! EXPR True if EXPR is false
    EXPR1 -a EXPR2 True if both EXPR1 and EXPR2 are true
    EXPR1 -o EXPR2 True if either EXPR1 or EXPR2 is true

    Examples

    How to Use These Examples

    The examples below show common ways to use the test command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    # Test if a file exists test -f /etc/passwd && echo "File exists"
    # Alternative syntax using square brackets [ -f /etc/passwd ] && echo "File exists"
    # Test if a directory exists test -d /etc || echo "Directory doesn't exist"
    # Test if a variable is empty test -z "$VAR" && echo "Variable is empty or not set"
    # Advanced Examples Advanced
    # Test if a file exists and is readable if [ -f /etc/passwd ] && [ -r /etc/passwd ]; then echo "File exists and is readable" fi # Compare string equality if [ "$str1" = "$str2" ]; then echo "Strings are equal" fi # Compare string inequality if [ "$str1" != "$str2" ]; then echo "Strings are not equal" fi # Check if a string contains another string if [[ "$str1" == *"$str2"* ]]; then echo "str1 contains str2" fi # Check if a variable is defined and not empty if [ -n "$VAR" ]; then echo "Variable is defined and not empty" fi # Compare numbers if [ "$num1" -eq "$num2" ]; then echo "Numbers are equal" fi # Check if file1 is newer than file2 if [ file1 -nt file2 ]; then echo "file1 is newer than file2" fi # Complex logical operations if [ "$a" -eq 1 ] && [ "$b" -eq 2 ] || [ "$c" -eq 3 ]; then echo "Complex condition met" fi # Test for symbolic link if [ -L /path/to/link ]; then echo "This is a symbolic link" fi # Test if user has execute permission on a file if [ -x /path/to/script.sh ]; then echo "File is executable" fi # Check if two files are the same (through hard or symbolic links) if [ file1 -ef file2 ]; then echo "file1 and file2 refer to the same file" fi

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `test` command is a fundamental utility in Unix and Linux shell scripting, used to evaluate conditional expressions. It provides a way to test file attributes, compare strings, check values, and evaluate logical expressions. The results of these tests are typically used in shell scripts to make decisions about program flow. There are two primary ways to use the `test` command: 1. Using the keyword `test` followed by the expression to evaluate: `test expression` 2. Using square brackets, which is a synonym for test: `[ expression ]` In modern shells like Bash, there's also an extended test command using double square brackets `[[ expression ]]`, which provides additional functionality and more intuitive syntax for complex expressions. The `test` command is especially useful for: - **Checking file properties**: Determining if files exist, their types, permissions, and relationships - **String operations**: Checking if strings are empty, equal, or contain certain patterns - **Numeric comparisons**: Comparing integers for equality, greater than, less than relationships - **Logical operations**: Combining multiple conditions with AND, OR, and NOT operations The command returns a status of 0 (true) or 1 (false) based on the evaluation of the expression, which makes it perfect for use in shell conditionals like `if`, `while`, and as part of logical operators `&&` (AND) and `||` (OR). In shell scripts, the `test` command is most commonly seen in `if` statements: ```bash if [ -f file.txt ]; then echo "file.txt exists" fi ``` Or in one-liners using logical operators: ```bash [ -d /tmp ] && echo "Directory exists" || echo "Directory doesn't exist" ``` It's important to note that spacing is critical when using the bracket syntax. There must be spaces after the opening bracket and before the closing bracket: `[ expression ]`, not `[expression]`. While the `test` command might seem simple, it's one of the most frequently used utilities in shell scripting, forming the foundation of conditional logic in scripts. Understanding its various options and expressions is essential for effective shell scripting in Unix and Linux environments.

    Related Commands

    These commands are frequently used alongside test or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the test command works in different scenarios.

    $ test
    View All Commands