test - macOS


Overview

test is a command-line utility for evaluating expressions and performing conditional execution in shell scripts. It allows for complex decision-making and conditional branching based on various conditions and operators.

Syntax

test [OPTIONS] EXPRESSION

EXPRESSION can be any valid shell expression, including:

  • Conditional expressions (e.g., [ -f file ])
  • File comparisons (e.g., [ file1 -nt file2 ])
  • String comparisons (e.g., [ "string1" = "string2" ])
  • Logical operators (e.g., &&, ||)
  • Parentheses for grouping (e.g., [ (expression1) -a (expression2) ])

Options/Flags

  • -a (and): Evaluate both expressions and return true only if both are true.
  • -o (or): Evaluate both expressions and return true if either is true.
  • -n STRING (non-zero length): Return true if STRING is not empty.
  • -z STRING (zero length): Return true if STRING is empty.
  • -s FILE (file exists): Return true if FILE exists and is not empty.
  • -f FILE (regular file): Return true if FILE is a regular file.
  • -d FILE (directory): Return true if FILE is a directory.
  • -x FILE (executable): Return true if FILE is executable.
  • -w FILE (writable): Return true if FILE is writable.
  • -r FILE (readable): Return true if FILE is readable.
  • -lt NUMBER (less than): Compare NUMBER with the exit status of the previous command. Return true if NUMBER is less than the exit status.
  • -le NUMBER (less than or equal): Similar to -lt, but returns true if NUMBER is less than or equal to the exit status.
  • -gt NUMBER (greater than): Similar to -lt, but returns true if NUMBER is greater than the exit status.
  • -ge NUMBER (greater than or equal): Similar to -lt, but returns true if NUMBER is greater than or equal to the exit status.

Examples

  • Check if a file exists and is executable:

    test -f /usr/bin/ls && test -x /usr/bin/ls
    
  • Check if a string is equal to “macOS”:

    test "uname" = "macOS"
    
  • Check the exit status of the previous command:

    ls -l && test $? -eq 0
    

Common Issues

  • Syntax errors: Double-check the syntax of your expressions and ensure that all brackets and parentheses are properly balanced.
  • Exit status: The exit status of test is 0 if the expression is true and 1 if it is false. Keep this in mind when using test in conditional statements.
  • Ambiguous expressions: Expressions can sometimes be ambiguous, leading to unexpected results. Use parentheses to group expressions explicitly and avoid ambiguity.

Integration

test can be used in conjunction with other shell commands and tools for advanced tasks, such as:

  • Conditional execution: Use test to control the flow of execution in shell scripts.
  • Script debugging: Use test to check the values of variables and debug scripts.
  • Complex filters: Combine test with find or grep to filter files or lines based on complex conditions.
  • [ (test): An older version of the test command.
  • expr (expression): A different expression evaluation utility with a different syntax.
  • [[ (conditional expression): A newer syntax for conditional expressions in bash.