Operators


test
Syntax
  • test <test-expr>
  • [<whitespace> <test-expr> <whitespace>]
  • <test-expr> = <test-expr> <-a,-o> <test-expr>
  • <test-expr> = <value1> <operator> <value2>
  • <test-expr> = <operator> <value2>
Usage
  • Compares two values
File test operators
  • -d : File is a directory
  • -e : File exists
  • -L : File is a symbolic link
  • -r : File is readable
  • -w : File is writeable
  • -x : File is executable
  • -nt : File1 is newer than file2
  • -ot : File1 is older than file2
Relational test operators (for comparing integer values)
  • -eq : Equal
  • -ne : Not equal
  • -lt : Less than
  • -le : Less than or equal
  • -gt : Greater than
  • -ge : Greater than or equal
Additional test operators
  • = : True if the strings are equal
  • != : True if the strings are not equal
  • ! : True if test is false
  • -a : True if both test1 and test2 are true
  • -o : True if either test1 or test2 are true

Examples
if [ -e data.txt ]; then
    echo "File exists"
fi

let

Syntax
  • let [<varname> =] <value1> <operator> <value2>
Usage
  • Blank spaces can only be used if the expression is in double quotes.
  • In an expression of the form let 1+2, the value of the expression is sent to standard output.
Examples
let arg=arg+1
let "var = arg * 3"

index="3"
while let "index > 0"; do
    echo while: $index
    let index=index-1
done