In 2025–2026, mastering shell script if else remains essential for writing smart, efficient Bash scripts. Conditional logic lets your scripts make decisions, validate input, check files, compare values, and handle different scenarios — all without running every line unnecessarily.
This comprehensive shell script if else guide covers syntax, real-world examples, arithmetic evaluation, file/string tests, elif chains, nested conditions, user input validation, and best practices. All examples are Bash-specific (/bin/bash) — some features like [[ ]], (( )), and =~ regex won’t work in strict POSIX sh/dash.
Key Takeaways – Shell Script if else Essentials
- it executes code blocks only when conditions are true (or false).
- Always use spaces inside [ condition ] — [ “$var” = “value” ] (correct), [“$var”=”value”] (wrong).
- Use (( )) for clean arithmetic: (( n % 2 == 0 )) — safer and more readable than [ ].
- elif handles multiple conditions cleanly; avoid deep nesting when possible.
- Common tests: -f (file exists), -r (readable), -z (empty string), -eq/-gt (numeric), =~ (regex in [[ ]]).
- Nested shell script if else solves complex logic; combine with logical operators &&/||.
- Validate user input with shell script if else + regex/pattern matching for robust scripts.
- Run scripts with bash script.sh to use Bash-only features like [[ ]] and (( )).
How Does Shell Script if else Work?
The basic syntax is:
if [ condition ]
then
# commands if true
else
# commands if false
fi
- if starts the block.
- [ condition ] is the test (must have spaces around brackets).
- then runs if condition is true.
- else runs if false (optional).
- fi ends the block (if backwards).
Condition examples:
- Numeric: [ “$a” -eq “$b” ]
- String: [ “$str” = “hello” ]
- File: [ -f “/path/file.txt” ]
How to Use Shell Script if else (Practical Examples)
1. Check if Two Numbers Are Equal
#!/bin/bash
a=5
b=5
if [ "$a" -eq "$b" ]
then
echo "Both numbers are equal"
else
echo "Numbers are different"
fi
Output: Both numbers are equal
2. Find Which Number Is Greater
#!/bin/bash
x=12
y=8
if [ "$x" -gt "$y" ]
then
echo "$x is greater than $y"
else
echo "$y is greater than $x"
fi
Output: 12 is greater than 8
3. Check if Number Is Even (Using Arithmetic (( )))
#!/bin/bash
num=10
if (( num % 2 == 0 ))
then
echo "$num is even"
else
echo "$num is odd"
fi
Output: 10 is even
Why (( ))? Cleaner syntax, no quotes needed, C-style operators (==, <, >, %).
4. Simple Password Prompt
#!/bin/bash
echo "Enter password:"
read -s pass # -s hides input
if [ "$pass" = "secret123" ]
then
echo "Access granted!"
else
echo "Access denied. Try again."
fi
5. Shell Script if else with elif – Grade Calculator
#!/bin/bash
score=87
if [ "$score" -ge 90 ]
then
echo "Grade: A"
elif [ "$score" -ge 80 ]
then
echo "Grade: B"
elif [ "$score" -ge 70 ]
then
echo "Grade: C"
else
echo "Grade: F"
fi
Output: Grade: B
6. Nested Shell Script if else – File Existence + Permissions
#!/bin/bash
file="report.pdf"
if [ -f "$file" ]
then
if [ -r "$file" ]
then
echo "File exists and is readable"
else
echo "File exists but is not readable"
fi
else
echo "File does not exist"
fi
7. Validate User Input (Numbers Only)
#!/bin/bash
echo "Enter a number:"
read input
if [[ "$input" =~ ^[0-9]+$ ]]
then
echo "$input is a valid number"
else
echo "Error: Please enter only digits"
fi
Shell Script if else – Best Practices (2025–2026)
- Always quote variables: [ “$var” = “value” ] prevents errors when empty.
- Use [[ ]] instead of [ ] for safer string/pattern matching (Bash-only).
- Prefer (( )) for math — no word-splitting issues.
- Use elif chains instead of deep nesting for readability.
- Test scripts with bash -n script.sh (syntax check) and shellcheck script.sh.
- Run with #!/bin/bash shebang to enable Bash features.
Shell Script if else – FAQ (2025–2026)
- How to write basic syntax?
Use if [ condition ]; then … else … fi — core of shell script if else. - How to use it for comparisons?
-eq, -ne, -gt, -lt, -ge, -le for numbers; =, != for strings. - What is elif ?
elif [ condition ]; then … — handles multiple conditions cleanly in shell script if else. - How to do arithmetic?
Use (( expression )) — example: (( num % 2 == 0 )) for even/odd checks. - How to check file existence?
[ -f “$file” ] — very common in this logic. - What is the difference between [ ] and [[ ]]?
[[ ]] is Bash-only, safer for strings/patterns, supports &&, ||, =~ regex.
Summary
You’ve mastered shell script if else: basic syntax, comparisons, arithmetic, file tests, elif, nesting, input validation, and best practices. Use shell script if else to make your Bash scripts intelligent and reliable.
Practice these examples — they’re the foundation of real-world automation, DevOps scripts, and system tools.
Recommended Resources
- Bash Conditional Expressions (Official GNU Bash Manual)
- ShellCheck – Lint your shell scripts online
- Advanced Bash-Scripting Guide (tlpd.org)
- Bash Pitfalls (wiki.bash-hackers.org)