Skip to main content

Simple shell conditional.

# To execute two different commands based on a condition:
if <command>; then <echo "true">; else <echo "false">; fi

# To check if a variable is defined:
if [[ -n "<$VARIABLE>" ]]; then <echo "defined">; else <echo "not defined">; fi

# To check if a file exists:
if [[ -f "<path/to/file>" ]]; then <echo "true">; else <echo "false">; fi

# To check if a directory exists:
if [[ -d "<path/to/directory>" ]]; then <echo "true">; else <echo "false">; fi

# To check if a file or directory exists:
if [[ -e "<path/to/file_or_directory>" ]]; then <echo "true">; else <echo "false">; fi

# To list all possible conditions ('test' is an alias to '['; both are commonly used with 'if'):
man [