0
0
Bash Scriptingscripting~10 mins

if-then-else in Bash Scripting - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the variable num is equal to 10.

Bash Scripting
if [ "$num" [1] 10 ]; then
  echo "Number is ten"
fi
Drag options to blanks, or click blank then click option'
A==
B=
C!=
D-eq
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' or '==' which are for strings, not integers.
Forgetting spaces around brackets.
2fill in blank
medium

Complete the code to print "Positive" if the variable num is greater than zero.

Bash Scripting
if [ "$num" [1] 0 ]; then
  echo "Positive"
fi
Drag options to blanks, or click blank then click option'
A-gt
B-lt
C-eq
D-ne
Attempts:
3 left
💡 Hint
Common Mistakes
Using string comparison operators instead of numeric ones.
Using -lt which means less than.
3fill in blank
hard

Fix the error in the if statement to correctly check if word equals "hello".

Bash Scripting
if [[ $word [1] "hello" ]]; then
  echo "Greeting detected"
fi
Drag options to blanks, or click blank then click option'
A-eq
B=
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using single equals = inside [[ ]] which is less common and can cause issues.
Using numeric operators like -eq for strings.
4fill in blank
hard

Fill both blanks to complete the if-else statement that prints "Even" if num is divisible by 2, else prints "Odd".

Bash Scripting
if [ $((num [1] 2)) [2] 0 ]; then
  echo "Even"
else
  echo "Odd"
fi
Drag options to blanks, or click blank then click option'
A%
B-eq
C-ne
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using division / instead of modulus %.
Using -ne instead of -eq.
5fill in blank
hard

Fill all three blanks to create a nested if-else that prints "Positive", "Zero", or "Negative" based on num.

Bash Scripting
if [ $num [1] 0 ]; then
  echo "Positive"
elif [ $num [2] 0 ]; then
  echo "Zero"
else
  echo "Negative"
fi
Drag options to blanks, or click blank then click option'
A-gt
B-eq
C-lt
D-ne
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the order of conditions.
Using string comparison operators instead of numeric ones.