0
0
Bash Scriptingscripting~20 mins

Logical operators (-a, -o, !) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Logical Operator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:00remaining
What is the output of this Bash test command?
Consider the following Bash command:
if [ 5 -gt 3 -a 2 -lt 4 ]; then echo "True"; else echo "False"; fi

What will be printed when this command runs?
Bash Scripting
if [ 5 -gt 3 -a 2 -lt 4 ]; then echo "True"; else echo "False"; fi
ASyntax error
BTrue
CFalse
DNo output
Attempts:
2 left
💡 Hint
Remember that '-a' means AND in Bash test expressions.
💻 Command Output
intermediate
1:00remaining
What does this Bash condition print?
Look at this Bash snippet:
if [ ! 0 -eq 1 ]; then echo "Yes"; else echo "No"; fi

What will be the output?
Bash Scripting
if [ ! 0 -eq 1 ]; then echo "Yes"; else echo "No"; fi
AYes
BNo
CSyntax error
DNo output
Attempts:
2 left
💡 Hint
The '!' operator negates the condition that follows it.
💻 Command Output
advanced
1:30remaining
What is the output of this complex Bash test?
Analyze this Bash command:
if [ 3 -lt 2 -o \( 4 -eq 4 -a ! 5 -gt 10 \) ]; then echo "Pass"; else echo "Fail"; fi

What will it print?
Bash Scripting
if [ 3 -lt 2 -o \( 4 -eq 4 -a ! 5 -gt 10 \) ]; then echo "Pass"; else echo "Fail"; fi
ANo output
BFail
CSyntax error
DPass
Attempts:
2 left
💡 Hint
Remember '-o' means OR, '-a' means AND, and '!' negates the condition. Parentheses group conditions.
💻 Command Output
advanced
1:00remaining
What error does this Bash test command raise?
What happens when you run this command?
if [ 5 -gt 3 -a ! ]; then echo "OK"; else echo "Not OK"; fi
Bash Scripting
if [ 5 -gt 3 -a ! ]; then echo "OK"; else echo "Not OK"; fi
APrints Not OK
BPrints OK
CSyntax error
DNo output
Attempts:
2 left
💡 Hint
The '!' operator must be followed by a condition.
💻 Command Output
expert
2:00remaining
What is the value of variable RESULT after running this script?
Given this Bash script:
RESULT=0
if [ ! \( 2 -eq 2 -o 3 -lt 1 \) ]; then RESULT=1; else RESULT=2; fi
echo $RESULT

What will be printed?
Bash Scripting
RESULT=0
if [ ! \( 2 -eq 2 -o 3 -lt 1 \) ]; then RESULT=1; else RESULT=2; fi
echo $RESULT
ASyntax error
B1
C2
D0
Attempts:
2 left
💡 Hint
Evaluate inside the parentheses first, then apply the negation.