if/Test Statement
test -e file
[ -e file ]
[[ -e file ]]
x=2
test $x -eq 1
[ $x -eq 2 ]
[[ $x -eq 3 ]]
File Test Operators
File status can be examined using several operators.
Parameter: Meaning
-s file size greater than zero
-r file exists and is readable
-w file exists and is writable
-x file exists and is executable
-u file exists and has the SUID bit set
-g file exists and has the SGID bit set
-k file exists and has the SVTX sticky bit set
-e file exists
-f file exists and is an ordinary file
-d file exists and is a directory
-c file exists as a character special file
-b file exists and is a named pipe file
-p file exists and is a named pipe file
-L file exists and is a symbolic link
-O file file exists and its owner is the effective user id
-G file file exists and its group is the effective group id
-S file file exists as a socket special file
file1 -ef file2 file1 is another name for file2
file1 -nt file2 file1 is newer than file2
file1 -ot file2 file1 is older than file2
-t des file descriptor des is open and associated with a terminal device
Numeric Expressions
For use with arithmetic
Expression Result
exp1 -eq exp2 exp1 is equal to exp2
exp1 -ne exp2 exp1 is not equal to exp2
expl -lt exp2 exp1 is less than exp2
exp1 -le exp2 exp1 is less than or equal to exp2
exp1 -gt exp2 exp1 is greater than exp2
exp1 -ge exp2 exp1 is greater than or equal to exp2
String Expressions
While examining strings use the following
Parameter Meaning
-n str string is non-zero in length
-z str string is zero in length
str1 = str2 str1 is the same as str2
str1 != str2 str1 is not the same as str2
[[ str = pattern ]] str matches pattern
[[ str != pattern ]] str does not match pattern
[[ str1 < str2 ]] str1 is before str2 in the ASCII collation seq.
[[ str1 > str2 ]] str1 is after str2 in the ASCII collation seq.
[[ -o opt ]] option opt is on for this shell


