tests and conditional expressions

The test command tests a condition. The return code is zero if the condition is true, 1 otherwise.

condition meaning
-e file true if file exists
-d file true if file exists and is a directory
-r file true if file exists and read access is true
-w file true if file exists and write access is true
-x file true if file exists and execution access is true
file1 -nt file2 true if file1 is newer (modification date) than file2
file1 -ot file2 true if file1 is older (modification date) than file2
string1 == string2 true if the string string1 equals string2
string1 != string2 true if the string string1 is different than string string2
#!/bin/bash
cat > f1 << END
toto
END
\rm -f f2

test -e f1
echo $?
test -e f2 || echo "f2 does not exist"
cp f1 f2
test f1 -nt f2 || echo f1 is newer
test f1 -ot f2 || echo f1 is older