Miscellaneous

test

Evaluate conditional expressions for use in shell scripts.

Synopsis

syntax
test EXPRESSION

Examples

Check if file exists
test -f /etc/hosts && echo 'exists'
Check if directory exists (bracket syntax)
[ -d /tmp ] && echo 'is directory'
String comparison
test "$USER" = 'root' && echo 'root'
Check argument count
[ $# -gt 0 ] && echo 'has arguments'

Common options

FlagDescription
-fTrue if file exists and is regular
-dTrue if directory exists
-zTrue if string is empty
-nTrue if string is non-empty
-eqInteger equal
-gtInteger greater than

About test

The `test` command evaluate conditional expressions for use in shell scripts. Utility commands cover a broad range of everyday tasks from date manipulation and arithmetic to terminal control and system documentation.

These tools fill in the gaps between major categories and are frequently used in shell scripts, cron jobs, and interactive sessions. The command accepts 6 commonly used flags shown above, though the full set of options is available in the man page (`man test`).

The 4 examples on this page cover typical real-world usage patterns that you can copy and adapt for your own workflows.

Related commands

More Miscellaneous Commands

Other commands in the Miscellaneous category

Related tools