test
ShellAdvanced In simulator In real LinuxEvaluate a condition (also written [ ... ])
Name: test; [ is another name for it
You can practise it right away in the simulator, and use it in real Linux mode too.
Description
test evaluates a conditional expression and exits 0 if it holds, 1 otherwise. [ is the same command with a required closing ], which is why the spaces inside the brackets matter.
It tests files (-f, -d, -r …), strings (=, !=, -z, -n) and integers (-eq, -lt …). In bash, [[ ]] is usually preferred.
Syntax
test EXPRESSIONCommon options
| -e | The file exists (any kind) |
| -f | File exists and is a regular file |
| -d | Directory exists |
| -L | It is a symbolic link |
| -s | The file exists and is not empty |
| -r / -w / -x | You may read / write / execute it |
| -nt / -ot | Newer / older than another file: [ a -nt b ] |
| -z | String is empty |
| -n | The string is not empty |
| = / != | Strings equal / not equal (spaces around =) |
| -eq -lt -gt | Integer comparison: equal, less, greater |
| ! / -a / -o | Not / and / or |