Bird
Raised Fist0
Bash Scriptingscripting~10 mins

if-then-fi structure in Bash Scripting - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - if-then-fi structure
Start
↓
Evaluate condition
↓
Execute commands inside then
↓
End if
↓
Skip commands inside then
↓
End if
The script checks a condition. If true, it runs commands inside then. If false, it skips them and ends the if block.
Execution Sample
Bash Scripting
if [ "$num" -gt 0 ]; then
  echo "Positive number"
fi
Checks if num is greater than 0; if yes, prints 'Positive number'.
Execution Table
StepConditionResultBranch TakenOutput
1[ "$num" -gt 0 ]TrueExecute then blockPositive number
2End of if-Exit if-
💡 Condition false or commands inside then executed; if block ends with fi.
Variable Tracker
VariableStartAfter Step 1Final
num555
Key Moments - 2 Insights
Why does the script skip the echo command sometimes?
Because the condition in the if statement is false, so the then block is not executed (see execution_table step 1).
What does 'fi' mean in the script?
'fi' marks the end of the if block, telling the shell where the conditional commands finish.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens when the condition is true?
AThe commands inside then block are skipped
BThe commands inside then block run
CThe script ends immediately
DThe condition is checked again
💡 Hint
See execution_table row 1, 'Branch Taken' and 'Output' columns
At which step does the script finish the if block?
AStep 2
BStep 1
CBefore Step 1
DAfter Step 2
💡 Hint
Check execution_table row 2, 'Step' and 'Branch Taken' columns
If num was -3, what would happen at step 1?
ACondition true, then block runs
BScript crashes
CCondition false, then block skipped
DCondition true, but then block skipped
💡 Hint
Refer to variable_tracker for num value and execution_table step 1 condition result
Concept Snapshot
if-then-fi structure in bash:
if [ condition ]; then
  commands
fi
- Checks condition
- Runs commands if true
- Ends with fi
- Skips commands if false
Full Transcript
This visual execution shows how the bash if-then-fi structure works. The script starts by evaluating a condition inside the if statement. If the condition is true, it executes the commands inside the then block. If false, it skips those commands. The if block ends with fi. Variables like num keep their values unchanged during this process. Key points include understanding that fi marks the end of the if block and that commands inside then only run when the condition is true. The execution table traces these steps clearly, showing when commands run or are skipped.

Practice

(1/5)
1. What is the purpose of the if-then-fi structure in a bash script?
easy
A. To run commands only if a certain condition is true
B. To repeat commands multiple times
C. To define a function
D. To declare a variable

Solution

  1. Step 1: Understand the role of if-then-fi

    The if-then-fi structure checks a condition and runs commands only if that condition is true.
  2. Step 2: Compare with other options

    Repeating commands is done with loops, functions define reusable code, and variables store data, so these do not match if-then-fi.
  3. Final Answer:

    To run commands only if a certain condition is true -> Option A
  4. Quick Check:

    if-then-fi controls conditional execution = D [OK]
Hint: If you want to do something only when true, use if-then-fi [OK]
Common Mistakes:
  • Confusing if-then-fi with loops
  • Thinking if-then-fi defines functions
  • Using if-then-fi to declare variables
2. Which of the following is the correct syntax for a simple if statement in bash?
easy
A. if $x > 5 then echo "Yes" fi
B. if ($x > 5) { echo "Yes" }
C. if [ $x -gt 5 ]; then echo "Yes"; fi
D. if $x -gt 5 echo "Yes" fi

Solution

  1. Step 1: Check the correct if syntax in bash

    Bash requires conditions inside square brackets with spaces, then 'then', commands, and ending with 'fi'. if [ $x -gt 5 ]; then echo "Yes"; fi matches this.
  2. Step 2: Identify errors in other options

    if ($x > 5) { echo "Yes" } uses wrong syntax with parentheses and braces (not bash). if $x > 5 then echo "Yes" fi misses semicolons and brackets. if $x -gt 5 echo "Yes" fi misses 'then' keyword.
  3. Final Answer:

    if [ $x -gt 5 ]; then echo "Yes"; fi -> Option C
  4. Quick Check:

    Correct bash if syntax = C [OK]
Hint: Remember: if [ condition ]; then ... fi with spaces [OK]
Common Mistakes:
  • Missing spaces inside [ ]
  • Omitting 'then' keyword
  • Using wrong brackets or symbols
3. What will be the output of this script?
#!/bin/bash
x=10
if [ $x -lt 5 ]; then
echo "Less"
fi
echo "Done"
medium
A. No output
B. Less Done
C. Less
D. Done

Solution

  1. Step 1: Evaluate the condition

    The variable x is 10. The condition checks if 10 is less than 5, which is false.
  2. Step 2: Determine which commands run

    Since the condition is false, the echo "Less" inside if does not run. The echo "Done" after if runs always.
  3. Final Answer:

    Done -> Option D
  4. Quick Check:

    Condition false, only "Done" prints = A [OK]
Hint: If condition false, skip if block, run commands after fi [OK]
Common Mistakes:
  • Assuming if block runs even if condition false
  • Confusing -lt with -gt
  • Ignoring commands after fi
4. Identify the error in this bash script snippet:
if [ $num -eq 5 ]
then
  echo "Number is five"
fi
medium
A. Missing semicolon or newline after condition
B. No error, script is correct
C. Missing fi keyword
D. Wrong comparison operator

Solution

  1. Step 1: Check if syntax correctness

    The condition is inside [ ] with spaces, '-eq' is correct for numeric equality, 'then' is on next line, and 'fi' closes the block.
  2. Step 2: Confirm no syntax errors

    Newline after condition is valid instead of semicolon. All keywords are present and correctly placed.
  3. Final Answer:

    No error, script is correct -> Option B
  4. Quick Check:

    Proper if-then-fi syntax = B [OK]
Hint: Newline after condition is allowed instead of semicolon [OK]
Common Mistakes:
  • Thinking newline after condition is error
  • Confusing -eq with other operators
  • Forgetting fi keyword
5. You want to write a script that prints "Even" if a number stored in n is even, and "Odd" otherwise. Which of these is the correct if-then-fi structure to do this?
hard
A. if [ $((n % 2)) -eq 0 ]; then echo "Even"; else echo "Odd"; fi
B. if [ $n % 2 == 0 ]; then echo "Even"; else echo "Odd"; fi
C. if (( n % 2 == 0 )) then echo "Even"; else echo "Odd"; fi
D. if [ $n / 2 -eq 0 ]; then echo "Even"; else echo "Odd"; fi

Solution

  1. Step 1: Understand how to check evenness in bash

    We use arithmetic expansion $(( )) to calculate remainder of n divided by 2. If remainder is 0, number is even.
  2. Step 2: Analyze each option's correctness

    if [ $((n % 2)) -eq 0 ]; then echo "Even"; else echo "Odd"; fi correctly uses [ $((n % 2)) -eq 0 ] with then, else, fi. if [ $n % 2 == 0 ]; then echo "Even"; else echo "Odd"; fi uses invalid syntax inside [ ]. if (( n % 2 == 0 )) then echo "Even"; else echo "Odd"; fi misses semicolon before 'then' causing syntax error. if [ $n / 2 -eq 0 ]; then echo "Even"; else echo "Odd"; fi incorrectly uses division instead of modulo.
  3. Final Answer:

    if [ $((n % 2)) -eq 0 ]; then echo "Even"; else echo "Odd"; fi -> Option A
  4. Quick Check:

    Modulo with $(( )) inside [ ] and proper if-then-fi = A [OK]
Hint: Use $(( )) for math inside [ ] in if-then-fi [OK]
Common Mistakes:
  • Using % inside [ ] without $(( ))
  • Using == instead of -eq for numbers
  • Confusing division with modulo
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