String Operators
The string operators are:
- = Returns true if the strings are identical.
- != Returns true if the strings are NOT identical. String returns true if the string is not null.
- -z Returns true if the string is null.
Note: There is another operator, -n, which we will not use. It is often mis-documented and causes confusion.
Integer Operators
The integer operators are binary operators. They are preceded and followed by integer literals, variables or expressions that result in an integer. An error is generated if there is not an integer value before and after, so do not use with unset variables.
- -eq Returns true if integer values are equal.
- -ne Returns true if integer values are not equal.
- -gt Returns true if 1st value is greater than the 2nd value.
- -lt Returns true if 1st value is less than the 2nd value.
- -ge Returns true if 1st value is greater than or equal to the 2nd value.
- -le Returns true if 1st value is less than or equal to the 2nd value.
Remember: valid integer values must be on both sides of the operator. Examples:
- test $var1 -eq $var2: True if both variables hold the same integer values.
- [ $var1 -eq $var2 ]: True if var1 is greater than or equal to var2.
- test 100 -le `wc -l file1`: True if file1 has 100 or more lines.
- test $var is not 0: True if var is not 0. Note: var could be a negative number.
File Operators
File operators are followed by a single argument. Multiple arguments will generate an error. If there is no argument or a variable after the operator that is empty, no error will be generated and the command will return true (I know, that just doesn't seem to make sense.). If you are looking at a soft link, the results are reported for the source or destination of the link (what the link is pointing at).
- -e Returns true if item exists.
- -f Returns true if is a file.
- -s Returns true if non-zero length (size).
- -r Returns true if readable.
- -w Returns true if writable.
- -x Returns true if executable.
- -d Returns true if is a directory.
- -L Returns true if is a soft link.
Examples:
- [ -d dir15 ]: True if dir15 exists and is a directory, otherwise false.
- test -r my_file: True if my_file exists and is readable by the user, otherwise false.
- [ -L my_new_file ]: True if my_new_file is a soft link. Otherwise false.