Skip to main content
This is tricky because Bash variables are string-like by default, but conditional expressions can compare values as either text or integers. So we have separate operators for strings and integers.

String Operators

String ordering is affected by the current locale. The clearest way to use < and > is inside [[ ]]:
They can also be used with [ ], but the operators must be escaped so the shell does not treat them as redirections:
Inside [ ], quoting variable expansions prevents word splitting and pathname expansion and safely handles empty values:
Inside [[ ]], quoting the left-hand variable is usually unnecessary because those expansions do not undergo word splitting or pathname expansion:

Integer Operators

File Operators

These check properties of files and directories. We use them a lot in scripts that interact with the filesystem:

Logical Operators

We combine them to build more complex conditions. Example:
${#var} gives us the string length directly. Using echo "$var" | wc -m would also count the newline printed by echo.