Here’s a quick reference guide to the tests performed on a variable as part of a shell script.
test expr or [ expr ]
Example (if $var is not set to any value)
if [[ -z $var ]]; then
echo “variable has no value”
fi
-n file true if variable has a value set
-z file true if variable is empty
-L true if file is symbolic link
file1 -nt file2 true if file1 newer than file2
file1 -ot file2 true if file1 older than file2
file1 -ef file2 true if file1 and file2 are same device and inode number.
-e file true if file exists (NOT ON HPUX – use f instead)
-x file true if file is executable
-r file true if file is readable
-w file true if file is writable
-f file true if file is a regular file
-d directory true if directory is a directory
-c file true if character special file
-b file true if block special file
-p file true if a named pipe
-u file true if set UID bit is set
-g file true if set GID bit is set
-k file true if sticky bit is set
-s file true if filesize > 0