What is the basic syntax of an if-then-else statement in Bash?
The basic syntax is:
if [ condition ]; then commands else other_commands fi
This checks the condition; if true, runs commands, else runs other_commands.
Click to reveal answer
beginner
How do you check if a file exists using if-then-else in Bash?
Use the test command with -e:
if [ -e filename ]; then echo "File exists" else echo "File does not exist" fi
Click to reveal answer
beginner
What does the 'fi' keyword do in a Bash if-then-else statement?
'fi' marks the end of the if statement. It is 'if' spelled backward and tells Bash where the if block finishes.
Click to reveal answer
beginner
Can you use multiple commands inside the then or else blocks? How?
Yes, you can put many commands inside then or else blocks by writing them on separate lines:
if [ condition ]; then command1 command2 else command3 command4 fi
Click to reveal answer
beginner
What happens if the condition in an if statement is false and there is no else block?
If the condition is false and no else block exists, Bash simply skips the then block and continues running the script after fi.
Click to reveal answer
Which keyword ends an if-then-else block in Bash?
Adone
Bend
Cfi
Dstop
✗ Incorrect
The keyword 'fi' is used to close an if statement in Bash.
What symbol is used to test a condition in an if statement?
A{}
B[]
C()
D<>
✗ Incorrect
Square brackets [] are used to test conditions in Bash if statements.
How do you write an else block in Bash?
Aelse { commands }
Belse: commands
Celse commands
Delse followed by commands on next lines
✗ Incorrect
In Bash, 'else' is followed by commands on the next lines before 'fi'.
What will this script print if the file 'test.txt' does not exist? if [ -e test.txt ]; then echo "Found"; else echo "Not found"; fi
ANot found
BNothing
CError
DFound
✗ Incorrect
If the file does not exist, the else block runs and prints 'Not found'.
Can you put multiple commands inside the then block?
AYes, separated by semicolons or new lines
BNo, only one command is allowed
COnly if you use curly braces
DOnly if you use parentheses
✗ Incorrect
Multiple commands can be placed inside then block separated by new lines or semicolons.
Explain how an if-then-else statement works in Bash with an example.
Think about checking a condition and running different commands based on true or false.
You got /6 concepts.
Describe how to check if a file exists using if-then-else in Bash and what happens if the file does not exist.
Use the test command inside the if condition.
You got /5 concepts.
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
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.
Step 2: Compare with other options
Repeating commands is done with loops, functions reuse code, and comments do not execute code.
Final Answer:
It lets the script choose between two paths based on a condition. -> Option D
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
Step 1: Recall bash if syntax
Bash requires spaces around brackets and a semicolon before then: if [ condition ]; then.
Step 2: Check other options
if(condition) then misses spaces and semicolon, B uses braces incorrectly, D misses brackets and semicolon.
Final Answer:
if [ condition ]; then -> Option A
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
Step 1: Evaluate the condition
The condition checks if x (5) is greater than 3, which is true.
Step 2: Determine which branch runs
Since condition is true, the script prints "Greater".
Final Answer:
Greater -> Option C
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
Step 1: Check if statement syntax
The if condition must be followed by then keyword to start the true block.
Step 2: Identify missing part
The script misses then after the condition line.
Final Answer:
Missing then after condition -> Option A
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
Step 1: Understand arithmetic evaluation in bash
Using double parentheses (( )) allows arithmetic expressions and comparisons directly.
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.
Final Answer:
if (( num % 2 == 0 )); then
echo "Even"
else
echo "Odd"
fi -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.