If / Else Conditional
SH · Bash Scriptingsyntax
if [[ condition ]]; then
commands
elif [[ condition ]]; then
commands
else
commands
fiexample
if [[ -f "/opt/app/config.yml" ]]; then
echo "Config found"
elif [[ -f "/etc/app/config.yml" ]]; then
echo "Using system config"
else
echo "No config found, using defaults"
exit 1
fiNote Use [[ ]] (double bracket) over [ ] for safer string comparisons and pattern matching. Common file tests: -f (file exists), -d (directory exists), -r (readable), -z (string is empty), -n (string is not empty). Use && and || inside [[ ]].