ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

Bash Shell Scripting Tutorial• 7 min read

Conditional Logic: [ ... ] vs [[ ... ]]

String comparisons (=, !=, -z), integer tests (-eq, -lt), and file checks (-f, -d)

Conditional statements in Bash use the test command or modern double brackets [[ ... ]] for string, integer, and file system evaluations.

Comparison Operators & Double Brackets

String comparisons: ==, !=, -z (checks if string is empty), -n (not empty).

Integer comparisons: -eq (equal), -ne (not equal), -lt (less than), -gt (greater than).

File system tests: -f (regular file exists), -d (directory exists), -s (file exists and not empty).

Best Practice: Always use [[ ... ]] over legacy [ ... ] because it prevents word splitting and supports regex matching (=~).

Production File Check Script
#!/usr/bin/env bash

CONFIG_FILE="/etc/app/config.json"

if [[ -f "$CONFIG_FILE" ]]; then
    echo "Configuration file found. Starting service..."
elif [[ -d "/etc/app" ]]; then
    echo "Directory exists, but config.json is missing!"
else
    echo "Error: Application is not installed."
    exit 1
fi
ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

Which file test operator in Bash checks if a path exists AND is a regular file?