Conditional Logic (if)
In Bash, the
if statement uses a specific syntax ending with fi (if backwards).bash
Wait! Notice the
[[ ]] brackets. This is the modern Bash "Test" syntax. Inside these brackets, you use specific operators for comparisons:-eq,-ne,-gt,-ge: Numeric comparisons.==,!=: String comparisons.-f,-d: File and Directory checks.
The case Statement
case is useful for matching a variable against multiple patterns. It ends with esac.bash
Loops in Shell
for: Iterating through a list or a range.while: Running as long as a command succeeds.
bash
Summary
| Keyword | Description |
|---|---|
| if / elif / else | Basic branching |
| [[ ]] | The "Test" operator (Modern) |
| case | Pattern matching branch |
| for | Iteration |
| while | Conditional loop |
| **&& / | |
| Logic | "Decision making based on command results" |
| Safety | Always use [[ ]] instead of [ ] for better safety and features |
| Modern | Concise syntax for complex file checks |
| Standard | Use (( )) for purely mathematical logic |
| Identity | Control flow in shell is based on the "Exit Code" of commands |