Bird
Raised Fist0
Bash Scriptingscripting~10 mins

if-elif-else in Bash Scripting - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - if-elif-else
Start
↓
Check if condition1
|Yes↓
Execute block1
↓
End
No↓
Check if condition2
|Yes↓
Execute block2
↓
End
No↓
Execute else block
↓
End
The script checks the first condition; if true, it runs its block and ends. Otherwise, it checks the next condition(s). If none match, it runs the else block.
Execution Sample
Bash Scripting
num=7
if [ $num -lt 5 ]; then
  echo "Less than 5"
elif [ $num -eq 7 ]; then
  echo "Equal to 7"
else
  echo "Other number"
fi
This script checks if num is less than 5, equal to 7, or something else, then prints a message.
Execution Table
StepCondition CheckedCondition ResultBranch TakenOutput
1[ $num -lt 5 ]FalseNo
2[ $num -eq 7 ]TrueYesEqual to 7
3elseN/ANo
💡 Condition [ $num -eq 7 ] is True, so the elif block runs and script ends.
Variable Tracker
VariableStartAfter Execution
num77
Key Moments - 2 Insights
Why does the script not print "Less than 5" even though it checks that condition first?
Because the condition [ $num -lt 5 ] is False (num is 7), so the script skips that block and checks the next condition as shown in step 1 and 2 of the execution_table.
What happens if none of the if or elif conditions are true?
The else block runs, as it is the fallback. This is shown in step 3 of the execution_table where else would execute if previous conditions were False.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output at step 2?
AEqual to 7
BLess than 5
COther number
DNo output
💡 Hint
Check the 'Output' column in row for step 2 in execution_table.
At which step does the script decide to run the elif block?
AStep 1
BStep 2
CStep 3
DNever
💡 Hint
Look at the 'Branch Taken' column and see where condition is True for elif.
If num was 3, how would the execution table change?
AStep 2 condition True, output 'Equal to 7'
BStep 3 else runs
CStep 1 condition True, output 'Less than 5', no further checks
DNo output at all
💡 Hint
Refer to variable_tracker and imagine num=3, then check condition results in execution_table.
Concept Snapshot
if-elif-else in bash:
if [ condition1 ]; then
  commands
elif [ condition2 ]; then
  commands
else
  commands
fi

Checks conditions in order; runs first true block; else runs if none true.
Full Transcript
This visual execution shows how bash if-elif-else works. The script starts by checking the first condition. If it is true, it runs that block and ends. If false, it checks the next condition. If none are true, it runs the else block. In the example, num=7 is checked against less than 5 (false), then equal to 7 (true), so it prints 'Equal to 7' and stops. Variables stay the same during checks. This helps beginners see exactly how conditions are tested and which block runs.

Practice

(1/5)
1. What does the if-elif-else structure do in a Bash script?
easy
A. It repeats a set of commands multiple times.
B. It comments out lines of code.
C. It defines a function to reuse code.
D. It lets the script choose between multiple actions based on conditions.

Solution

  1. Step 1: Understand the purpose of if-elif-else

    The if-elif-else structure checks conditions one by one and runs the matching block.
  2. Step 2: Compare with other options

    Loops repeat commands, functions reuse code, comments disable code lines. Only if-elif-else chooses actions.
  3. Final Answer:

    It lets the script choose between multiple actions based on conditions. -> Option D
  4. Quick Check:

    if-elif-else = choose actions [OK]
Hint: If you want choices, use if-elif-else [OK]
Common Mistakes:
  • Confusing if-elif-else with loops
  • Thinking it defines functions
  • Mixing it up with comments
2. Which of the following is the correct syntax to start an if-elif-else block in Bash?
easy
A. if [ condition ]; then
B. if (condition) {
C. if condition then
D. if condition:

Solution

  1. Step 1: Recall Bash if syntax

    Bash requires square brackets around conditions and 'then' after the condition, with semicolons or newlines.
  2. Step 2: Check each option

    if [ condition ]; then uses correct syntax: if [ condition ]; then. Others use wrong brackets or missing keywords.
  3. Final Answer:

    if [ condition ]; then -> Option A
  4. Quick Check:

    Correct if syntax = if [ condition ]; then [OK]
Hint: Use brackets and 'then' with semicolon or newline [OK]
Common Mistakes:
  • Omitting brackets around condition
  • Using parentheses instead of brackets
  • Missing 'then' keyword
3. What will be the output of this script?
#!/bin/bash
num=7
if [ $num -lt 5 ]; then
  echo "Less than 5"
elif [ $num -eq 7 ]; then
  echo "Equal to 7"
else
  echo "Other"
fi
medium
A. Less than 5
B. Equal to 7
C. Other
D. Syntax Error

Solution

  1. Step 1: Check the value of num and conditions

    num=7. First condition checks if num < 5 (false). Second checks if num == 7 (true).
  2. Step 2: Determine which block runs

    Since second condition is true, it prints "Equal to 7" and skips else.
  3. Final Answer:

    Equal to 7 -> Option B
  4. Quick Check:

    num=7 matches elif condition [OK]
Hint: Check conditions top to bottom, first true runs [OK]
Common Mistakes:
  • Assuming first condition runs
  • Confusing -lt and -eq operators
  • Missing fi causes syntax error
4. Identify the error in this Bash script snippet:
if [ $value -gt 10 ]
  echo "Greater than 10"
elif [ $value -eq 10 ]; then
  echo "Equal to 10"
else
  echo "Less than 10"
fi
medium
A. Missing fi at the end
B. Incorrect use of elif keyword
C. Missing 'then' after first if condition
D. Wrong comparison operator

Solution

  1. Step 1: Check syntax after if condition

    In Bash, 'then' must follow the if condition, either on same line after semicolon or next line.
  2. Step 2: Verify other parts

    elif has 'then', fi is present, operators are correct. Only missing 'then' after if.
  3. Final Answer:

    Missing 'then' after first if condition -> Option C
  4. Quick Check:

    if needs 'then' keyword [OK]
Hint: Always put 'then' after if condition [OK]
Common Mistakes:
  • Forgetting 'then' after if
  • Misplacing semicolons
  • Confusing elif syntax
5. You want a script to print "Positive", "Zero", or "Negative" based on a variable num. Which script correctly uses if-elif-else to do this?
hard
A. #!/bin/bash if [ $num -gt 0 ]; then echo "Positive" elif [ $num -eq 0 ]; then echo "Zero" else echo "Negative" fi
B. #!/bin/bash if [ $num > 0 ]; then echo "Positive" elif [ $num == 0 ]; then echo "Zero" else echo "Negative" fi
C. #!/bin/bash if $num > 0 then echo "Positive" elif $num == 0 then echo "Zero" else echo "Negative" fi
D. #!/bin/bash if [ $num -ge 0 ]; then echo "Positive" elif [ $num -eq 0 ]; then echo "Zero" else echo "Negative" fi

Solution

  1. Step 1: Check comparison operators

    Bash uses -gt, -eq, -lt for numeric comparisons. #!/bin/bash if [ $num -gt 0 ]; then echo "Positive" elif [ $num -eq 0 ]; then echo "Zero" else echo "Negative" fi uses correct operators.
  2. Step 2: Verify logic and syntax

    #!/bin/bash if [ $num -gt 0 ]; then echo "Positive" elif [ $num -eq 0 ]; then echo "Zero" else echo "Negative" fi correctly checks >0, ==0, else negative. Others use wrong operators or syntax.
  3. Final Answer:

    A -> Option A
  4. Quick Check:

    Use -gt, -eq for numbers in if-elif-else [OK]
Hint: Use -gt, -eq for numbers; avoid > or == in [ ] [OK]
Common Mistakes:
  • Using > or == inside [ ]
  • Missing then keyword
  • Wrong operator for numeric comparison
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard