Problem Statement
What is the correct syntax for an if statement in bash?
Explanation
Bash if statement syntax: if [ condition ]; then commands; fi. The spaces around brackets are required. Semicolon or newline separates condition from then. Alternative format:
```bash
if [ condition ]
then
commands
fi
```
Both formats valid, choose based on readability preference.
Add else: if [ condition ]; then commands1; else commands2; fi. Add elif for multiple conditions: if [ cond1 ]; then cmd1; elif [ cond2 ]; then cmd2; else cmd3; fi. Test command [ ] is equivalent to test command: if test condition; then commands; fi. Modern bash prefers [[ ]] with more features: [[ $VAR == pattern ]] supports pattern matching.
Common conditions: [ "$VAR" = "value" ] string equality, [ $NUM -eq 5 ] numeric equality, [ -f file ] file exists, [ $A -gt $B ] greater than. Always quote variables to handle empty values safely: [ "$VAR" = "value" ] prevents errors if VAR empty. Understanding if statements is fundamental for conditional logic in scripts.
