File & String Tests in Bash

From the Bash Scripting cheat sheet · Conditionals · verified Jul 2026

File & String Tests

Common test operators.

bash
# Quick Reference
[[ -f file ]]   # regular file exists
[[ -d dir ]]    # directory exists
[[ -z "$s" ]]   # string is empty
[[ "$a" == "$b" ]]  # strings equal
💡 -f tests a regular file; -e tests any path; -d tests a directory.
⚡ Always quote the value in string tests ([[ -z "$s" ]]) to handle empties safely.
📌 Use -eq/-lt/-gt for numbers and ==/</> for strings - mixing them miscompares.
🟢 (( n > 5 )) reads more naturally than [[ "$n" -gt 5 ]] for pure number checks.
testsfiles

More Bash tasks

Back to the full Bash Scripting cheat sheet