Skip to content

Bash shell

Expression reference

This page is really useful. Here are some highlights of my most common needs:

ExpressionMeaningPersonal comments
[ -f FILE ]True if FILE exists and is a regular fileUse this by default instead of [ -a FILE ]
[ -d PATH ]True if PATH exists and is a directory
[ -n STRING ]True if length of STRING is non-zero
[ -z STRING ]True if length of STRING is zero
[ ! EXPR ]True if EXPR is false.Better to use inherently-negative assertions if available, e.g. -z STR instead of ! -n STR
[ EXPR1 -a EXPR2 ]True if both EXPR1 and EXPR2 are true.
[ EXPR1 -o EXPR2 ]True if either EXPR1 or EXPR2 is true.

This work is licensed under CC BY-NC-ND 4.0