Bird
Raised Fist0
Bash Scriptingscripting~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
If-Then-Else Bash Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of nested if-then-else in Bash
What is the output of this Bash script when run?
#!/bin/bash
x=5
if [ $x -gt 3 ]; then
  if [ $x -lt 10 ]; then
    echo "Between 4 and 9"
  else
    echo "10 or more"
  fi
else
  echo "3 or less"
fi
Bash Scripting
#!/bin/bash
x=5
if [ $x -gt 3 ]; then
  if [ $x -lt 10 ]; then
    echo "Between 4 and 9"
  else
    echo "10 or more"
  fi
else
  echo "3 or less"
fi
ABetween 4 and 9
B10 or more
C3 or less
DSyntax error
Attempts:
2 left
💡 Hint
Check the conditions step by step and see which echo runs.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this if-then-else script
Which option shows the correct way to fix the syntax error in this Bash script?
#!/bin/bash
x=10
if [ $x -eq 10 ]
  echo "x is 10"
else
  echo "x is not 10"
fi
Bash Scripting
#!/bin/bash
x=10
if [ $x -eq 10 ]
  echo "x is 10"
else
  echo "x is not 10"
fi
AAdd 'then' after the if condition: if [ $x -eq 10 ]; then
BAdd a semicolon after echo statement
CReplace 'fi' with 'end'
DRemove the else block
Attempts:
2 left
💡 Hint
Bash requires 'then' after the if condition.
🔧 Debug
advanced
2:00remaining
Why does this if-then-else script always print 'No'?
Consider this Bash script:
#!/bin/bash
var=""
if [ $var ]; then
  echo "Yes"
else
  echo "No"
fi

Why does it always print "No" even if var is empty?
Bash Scripting
#!/bin/bash
var=""
if [ $var ]; then
  echo "Yes"
else
  echo "No"
fi
ABecause the script has a syntax error
BBecause [ $var ] is false when var is empty, so else runs
CBecause var is not defined
DBecause echo is not inside the if block
Attempts:
2 left
💡 Hint
Check how Bash treats empty strings inside [ ]
🚀 Application
advanced
2:00remaining
What is the output of this if-then-else with arithmetic test?
What does this Bash script print?
#!/bin/bash
num=7
if (( num % 2 == 0 )); then
  echo "Even"
else
  echo "Odd"
fi
Bash Scripting
#!/bin/bash
num=7
if (( num % 2 == 0 )); then
  echo "Even"
else
  echo "Odd"
fi
ASyntax error
BEven
COdd
DNo output
Attempts:
2 left
💡 Hint
Check if 7 is divisible by 2
🧠 Conceptual
expert
3:00remaining
How many times does this loop print 'Hello'?
Analyze this Bash script:
#!/bin/bash
count=0
while [ $count -lt 3 ]; do
  if [ $count -eq 1 ]; then
    count=$((count + 2))
  else
    count=$((count + 1))
  fi
  echo "Hello"
done

How many times will "Hello" be printed?
Bash Scripting
#!/bin/bash
count=0
while [ $count -lt 3 ]; do
  if [ $count -eq 1 ]; then
    count=$((count + 2))
  else
    count=$((count + 1))
  fi
  echo "Hello"
done
A4
B3
CInfinite loop
D2
Attempts:
2 left
💡 Hint
Trace the value of count each loop iteration.

Practice

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

Solution

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

    The if-then-else structure checks a condition and runs code based on whether the condition is true or false.
  2. Step 2: Compare with other options

    Repeating commands is done with loops, functions reuse code, and comments do not execute code.
  3. Final Answer:

    It lets the script choose between two paths based on a condition. -> Option D
  4. Quick Check:

    if-then-else = choose path [OK]
Hint: If you see a condition, it's likely if-then-else [OK]
Common Mistakes:
  • Confusing if-then-else with loops
  • Thinking if-then-else defines functions
  • Mixing comments with control flow
2. Which of the following is the correct syntax to start an if statement in bash?
easy
A. if [ condition ]; then
B. if { condition } then
C. if(condition) then
D. if condition then

Solution

  1. Step 1: Recall bash if syntax

    Bash requires spaces around brackets and a semicolon before then: if [ condition ]; then.
  2. Step 2: Check other options

    if(condition) then misses spaces and semicolon, B uses braces incorrectly, D misses brackets and semicolon.
  3. Final Answer:

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

    Spaces and semicolon needed [OK]
Hint: Remember spaces around [ ] and semicolon before then [OK]
Common Mistakes:
  • No spaces around brackets
  • Missing semicolon before then
  • Using parentheses or braces incorrectly
3. What will be the output of this script?
#!/bin/bash
x=5
if [ $x -gt 3 ]; then
  echo "Greater"
else
  echo "Smaller"
fi
medium
A. No output
B. Smaller
C. Greater
D. Syntax Error

Solution

  1. Step 1: Evaluate the condition

    The condition checks if x (5) is greater than 3, which is true.
  2. Step 2: Determine which branch runs

    Since condition is true, the script prints "Greater".
  3. Final Answer:

    Greater -> Option C
  4. Quick Check:

    5 > 3 = Greater [OK]
Hint: Check numeric comparison carefully [OK]
Common Mistakes:
  • Confusing -gt with -lt
  • Missing spaces causing syntax errors
  • Assuming else runs when condition is true
4. Find the error in this script:
#!/bin/bash
if [ $num -eq 10 ]
  echo "Ten"
else
  echo "Not Ten"
fi
medium
A. Missing then after condition
B. Wrong comparison operator
C. Missing fi at the end
D. No error

Solution

  1. Step 1: Check if statement syntax

    The if condition must be followed by then keyword to start the true block.
  2. Step 2: Identify missing part

    The script misses then after the condition line.
  3. Final Answer:

    Missing then after condition -> Option A
  4. Quick Check:

    if needs then [OK]
Hint: Always put then after condition in if [ ] [OK]
Common Mistakes:
  • Forgetting then keyword
  • Using wrong comparison operators
  • Missing fi to close if
5. You want a script that prints "Even" if a number is even, and "Odd" if it is odd. Which script correctly uses if-then-else to do this?
num=4
# Choose correct script
hard
A. if [ $num % 2 -eq 0 ]; then echo "Even" else echo "Odd" fi
B. if (( num % 2 == 0 )); then echo "Even" else echo "Odd" fi
C. if [ $num % 2 == 0 ]; then echo "Even" else echo "Odd" fi
D. if [ $num / 2 -eq 0 ]; then echo "Even" else echo "Odd" fi

Solution

  1. Step 1: Understand arithmetic evaluation in bash

    Using double parentheses (( )) allows arithmetic expressions and comparisons directly.
  2. Step 2: Check each option

    if [ $num % 2 -eq 0 ]; then echo "Even" else echo "Odd" fi uses % inside [ ] without arithmetic evaluation. if [ $num % 2 == 0 ]; then echo "Even" else echo "Odd" fi uses % inside [ ] without arithmetic evaluation. if [ $num / 2 -eq 0 ]; then echo "Even" else echo "Odd" fi uses / instead of % for modulus. if (( num % 2 == 0 )); then echo "Even" else echo "Odd" fi correctly uses (( )) with modulus and comparison.
  3. Final Answer:

    if (( num % 2 == 0 )); then echo "Even" else echo "Odd" fi -> Option B
  4. Quick Check:

    Use (( )) for arithmetic if [OK]
Hint: Use (( )) for math conditions in bash [OK]
Common Mistakes:
  • Using [ ] with arithmetic operators incorrectly
  • Confusing / with % for modulus
  • Missing then keyword
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