Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Why conditionals branch script logic in Bash Scripting - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Conditional Logic Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Bash script with conditionals?
Consider the following Bash script. What will it print when run?
Bash Scripting
count=5
if [ $count -gt 3 ]; then
  echo "Count is greater than 3"
else
  echo "Count is 3 or less"
fi
ASyntax error
BCount is 3 or less
CCount is greater than 3
DNo output
Attempts:
2 left
💡 Hint
Check the condition if count is greater than 3.
💻 Command Output
intermediate
2:00remaining
What output does this nested conditional produce?
What will this Bash script print?
Bash Scripting
value=10
if [ $value -lt 5 ]; then
  echo "Less than 5"
elif [ $value -eq 10 ]; then
  echo "Equal to 10"
else
  echo "Other value"
fi
AEqual to 10
BLess than 5
COther value
DSyntax error
Attempts:
2 left
💡 Hint
Check the elif condition carefully.
🔧 Debug
advanced
2:00remaining
Why does this Bash script fail to run?
This script is intended to print "Positive" if the number is greater than zero, but it fails. What is the error?
Bash Scripting
num=3
if [ $num -gt 0 ]; then
  echo "Positive"
fi
ANo output because condition is false
BRuns correctly and prints "Positive"
CRuntime error: variable not defined
DSyntax error due to incorrect comparison operator
Attempts:
2 left
💡 Hint
Check the operator used inside the square brackets.
💻 Command Output
advanced
2:00remaining
What does this script print with multiple conditions?
What is the output of this Bash script?
Bash Scripting
score=75
if [ $score -ge 90 ]; then
  echo "Grade A"
elif [ $score -ge 80 ]; then
  echo "Grade B"
elif [ $score -ge 70 ]; then
  echo "Grade C"
else
  echo "Grade F"
fi
AGrade C
BGrade A
CGrade B
DGrade F
Attempts:
2 left
💡 Hint
Check which condition matches the score first.
💻 Command Output
expert
2:00remaining
What is the output of this Bash script using logical AND in condition?
What will this script print?
Bash Scripting
a=5
b=10
if [ $a -lt 10 ] && [ $b -gt 5 ]; then
  echo "Both conditions true"
else
  echo "One or both conditions false"
fi
AOne or both conditions false
BBoth conditions true
CSyntax error due to missing semicolon
DNo output
Attempts:
2 left
💡 Hint
Both conditions must be true for the if to run.

Practice

(1/5)
1. What is the main purpose of using if statements in a bash script?
easy
A. To make decisions and run different commands based on conditions
B. To repeat commands multiple times
C. To define functions
D. To declare variables

Solution

  1. Step 1: Understand the role of if in scripts

    if statements check conditions and decide which commands to run next.
  2. Step 2: Compare with other options

    Repeating commands is done by loops, functions define reusable code, and variables store data, so these are not the main purpose of if.
  3. Final Answer:

    To make decisions and run different commands based on conditions -> Option A
  4. Quick Check:

    Conditionals branch logic = A [OK]
Hint: If it chooses what to do next, it's an if statement [OK]
Common Mistakes:
  • Confusing if with loops
  • Thinking if defines functions
  • Mixing variable declaration with conditionals
2. Which of the following is the correct syntax to start an if statement in bash?
easy
A. if condition {}
B. if (condition) {
C. if condition then
D. if [ condition ]; then

Solution

  1. Step 1: Recall bash if syntax

    Bash requires square brackets around the condition and a semicolon before then.
  2. Step 2: Check other options for syntax errors

    Options A, B, and C use incorrect syntax styles not valid in bash.
  3. Final Answer:

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

    Bash if syntax = D [OK]
Hint: Remember: if [ condition ]; then is bash style [OK]
Common Mistakes:
  • Omitting semicolon before then
  • Using parentheses instead of brackets
  • Missing brackets around condition
3. What will be the output of this script?
#!/bin/bash
num=5
if [ $num -gt 10 ]; then
  echo "Greater than 10"
elif [ $num -eq 5 ]; then
  echo "Equals 5"
else
  echo "Less than 5"
fi
medium
A. Equals 5
B. Greater than 10
C. Less than 5
D. No output

Solution

  1. Step 1: Check the value of num

    The variable num is set to 5.
  2. Step 2: Evaluate the conditions in order

    First condition checks if 5 > 10 (false), second checks if 5 == 5 (true), so it prints "Equals 5".
  3. Final Answer:

    Equals 5 -> Option A
  4. Quick Check:

    num=5 matches elif condition = A [OK]
Hint: Check conditions top to bottom; first true runs [OK]
Common Mistakes:
  • Assuming first condition is true
  • Ignoring elif branch
  • Confusing numeric comparison operators
4. Identify the error in this bash conditional snippet:
if [ $value = 10 ]; then
  echo "Value is 10"
fi
medium
A. Missing semicolon before then
B. Using single equals for numeric comparison
C. Brackets should be double [[ ]]
D. No error, script runs fine

Solution

  1. Step 1: Understand comparison operators in bash

    For numeric comparison inside [ ], use -eq instead of =.
  2. Step 2: Check the snippet

    The snippet uses = which is for string comparison, so numeric comparison is incorrect here.
  3. Final Answer:

    Using single equals for numeric comparison -> Option B
  4. Quick Check:

    Numeric compare needs -eq not = [OK]
Hint: Use -eq for numbers, = for strings in [ ] [OK]
Common Mistakes:
  • Confusing string and numeric operators
  • Thinking = works for numbers
  • Ignoring bash operator rules
5. You want a script to print "Positive", "Zero", or "Negative" based on a variable num. Which conditional structure correctly branches all cases?
hard
A. if [ $num -ge 0 ]; then echo "Positive"; else echo "Negative"; fi
B. if [ $num = 0 ]; then echo "Zero"; else echo "Non-zero"; fi
C. if [ $num -gt 0 ]; then echo "Positive"; elif [ $num -eq 0 ]; then echo "Zero"; else echo "Negative"; fi
D. if [ $num -lt 0 ]; then echo "Negative"; elif [ $num -gt 0 ]; then echo "Positive"; fi

Solution

  1. Step 1: Identify all required cases

    We need to handle positive (>0), zero (=0), and negative (<0) values.
  2. Step 2: Check each option for coverage

    if [ $num -gt 0 ]; then echo "Positive"; elif [ $num -eq 0 ]; then echo "Zero"; else echo "Negative"; fi covers all three cases with if, elif, else branches. Others miss zero or one case.
  3. Final Answer:

    if [ $num -gt 0 ]; then echo "Positive"; elif [ $num -eq 0 ]; then echo "Zero"; else echo "Negative"; fi -> Option C
  4. Quick Check:

    All cases covered with if-elif-else = B [OK]
Hint: Use if-elif-else to cover all conditions [OK]
Common Mistakes:
  • Missing zero case
  • Using wrong comparison operators
  • Not using else for last case
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