Bird
Raised Fist0
Bash Scriptingscripting~20 mins

if-elif-else in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
If-Elif-Else Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of a simple if-elif-else script
What is the output of this Bash script when run with argument 15?
Bash Scripting
#!/bin/bash
num=$1
if [ "$num" -lt 10 ]; then
  echo "Less than 10"
elif [ "$num" -lt 20 ]; then
  echo "Between 10 and 19"
else
  echo "20 or more"
fi
ALess than 10
BSyntax error
C20 or more
DBetween 10 and 19
Attempts:
2 left
💡 Hint
Check the numeric comparison operators and the order of conditions.
🔧 Debug
intermediate
2:00remaining
Identify the error in this if-elif-else script
What error will this Bash script produce when run?
Bash Scripting
#!/bin/bash
num=5
if [ $num < 10 ]; then
  echo "Less than 10"
elif [ $num < 20 ]; then
  echo "Between 10 and 19"
else
  echo "20 or more"
fi
ARuns correctly and prints 'Less than 10'
BSyntax error due to incorrect comparison operator
CRuntime error: command not found
DPrints '20 or more'
Attempts:
2 left
💡 Hint
Check the comparison operators used inside the test brackets.
🚀 Application
advanced
2:30remaining
Script to categorize a number with if-elif-else
Which script correctly categorizes a number into 'small' (less than 5), 'medium' (5 to 10), or 'large' (greater than 10)?
A
#!/bin/bash
num=$1
if [ "$num" -lt 5 ]; then
  echo "small"
elif [ "$num" -le 10 ]; then
  echo "medium"
else
  echo "large"
fi
B
#!/bin/bash
num=$1
if [ "$num" -le 5 ]; then
  echo "small"
elif [ "$num" -lt 10 ]; then
  echo "medium"
else
  echo "large"
fi
C
#!/bin/bash
num=$1
if [ "$num" -lt 5 ]; then
  echo "small"
elif [ "$num" -lt 10 ]; then
  echo "medium"
else
  echo "large"
fi
D
#!/bin/bash
num=$1
if [ "$num" -lt 5 ]; then
  echo "small"
elif [ "$num" -gt 10 ]; then
  echo "medium"
else
  echo "large"
fi
Attempts:
2 left
💡 Hint
Check the numeric ranges carefully and the operators used.
🧠 Conceptual
advanced
1:30remaining
Understanding else placement in if-elif-else
What happens if you omit the else block in an if-elif-else chain in Bash?
AThe script runs but always executes the last elif block
BThe script throws a syntax error
CThe script runs without errors and does nothing if no conditions match
DThe script runs but skips all conditions
Attempts:
2 left
💡 Hint
Think about what else does in a conditional chain.
💻 Command Output
expert
2:30remaining
Output of nested if-elif-else with multiple conditions
What is the output of this script when run with argument 0?
Bash Scripting
#!/bin/bash
num=$1
if [ "$num" -gt 0 ]; then
  if [ "$num" -lt 10 ]; then
    echo "Positive single digit"
  else
    echo "Positive double digit or more"
  fi
elif [ "$num" -eq 0 ]; then
  echo "Zero"
else
  echo "Negative number"
fi
AZero
BPositive single digit
CNegative number
DSyntax error
Attempts:
2 left
💡 Hint
Trace the conditions carefully for input zero.

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