A useful testing feature provided by the shell is the capability to test file characteristics such as file type and permissions.
test -option filename
$ test -f funfile
$ echo $?
0
$ test -d funfile
$ echo $?
1
| Operation | Description |
|---|---|
| -d file | True if the file is a directory |
| -f file | True if the file exists and is a normal file (not a directory) |
| -s file | True if the file is more than zero bytes in length |
| -r file | True if the file is readable |
| -w file | True if the file is writable |
| -e file | True if the file exists |
| -L file | True if the file is a symbolic link |
| file1 -nt file2 | True if file1 is newer than file2 |
| file1 -ot file2 | True if file1 is older than file2 |
| -x file | True if the file is executable |
Logical operations can be performed on two expressions with one of the logical operators.
| Operation | Description |
|---|---|
| expr1 -o expr2 | Logical OR, true if either expr1 or expr2 is true |
| expr1 -a expr2 | Logical AND, true if both expr1 and expr2 are true |
| ! expr | Logical NOT, true if expr is false |