Bird
Raised Fist0
Bash Scriptingscripting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which logical operator in bash scripting means AND when used inside [ ]?
easy
A. &&
B. -o
C. !
D. -a

Solution

  1. Step 1: Understand bash logical operators inside [ ]

    Inside single brackets, -a means AND, -o means OR, and ! means NOT.
  2. Step 2: Identify the AND operator

    The operator -a is used to combine two conditions with AND logic.
  3. Final Answer:

    -a -> Option D
  4. Quick Check:

    AND operator = -a [OK]
Hint: Remember: -a is AND, -o is OR, ! is NOT inside [ ] [OK]
Common Mistakes:
  • Confusing -a with -o
  • Using && inside [ ] which is invalid
  • Thinking ! means AND
2. Which of the following is the correct syntax to check if file file.txt exists AND is readable in bash?
easy
A. [ -e file.txt -o -r file.txt ]
B. [ -e file.txt && -r file.txt ]
C. [ -e file.txt -a -r file.txt ]
D. [ ! -e file.txt -a -r file.txt ]

Solution

  1. Step 1: Understand the condition to check

    We want to check if file.txt exists AND is readable, so both conditions must be true.
  2. Step 2: Choose correct logical operator and syntax

    Inside [ ], -a means AND, so [ -e file.txt -a -r file.txt ] is correct. Using -o means OR, which is wrong here. Using && inside [ ] is invalid syntax.
  3. Final Answer:

    [ -e file.txt -a -r file.txt ] -> Option C
  4. Quick Check:

    AND check uses -a inside [ ] [OK]
Hint: Use -a for AND inside [ ], not && or -o for AND [OK]
Common Mistakes:
  • Using -o instead of -a for AND
  • Using && inside [ ] which causes syntax error
  • Negating conditions incorrectly with !
3. What will be the output of this script snippet?
if [ -f myfile.txt -o ! -r myfile.txt ]; then echo "Check passed"; else echo "Check failed"; fi

Assuming myfile.txt exists but is NOT readable.
medium
A. Check passed
B. Check failed
C. Syntax error
D. No output

Solution

  1. Step 1: Analyze conditions with given file state

    -f myfile.txt is true because file exists. ! -r myfile.txt is true because file is not readable.
  2. Step 2: Evaluate OR (-o) logic

    Since OR means either condition true passes, and both are true here, the whole condition is true.
  3. Final Answer:

    Check passed -> Option A
  4. Quick Check:

    True OR True = True [OK]
Hint: OR (-o) passes if any condition is true [OK]
Common Mistakes:
  • Thinking ! -r means readable
  • Confusing -o with -a
  • Expecting syntax error due to !
4. Identify the error in this bash condition:
if [ !-f myfile.txt -a -r myfile.txt ]; then echo "Valid"; fi
medium
A. Missing spaces around ! operator
B. Incorrect use of -a inside [ ]
C. No error, condition is correct
D. Missing closing bracket ]

Solution

  1. Step 1: Check spacing around ! operator

    In bash, ! must have a space after it inside [ ]. The code [ ! -f myfile.txt -a -r myfile.txt ] is correct only if spaces are present. If missing, it causes error.
  2. Step 2: Confirm other syntax parts

    The use of -a inside [ ] is valid. The closing bracket is present. So no error there.
  3. Final Answer:

    Missing spaces around ! operator -> Option A
  4. Quick Check:

    ! needs spaces inside [ ] [OK]
Hint: Always put spaces around ! inside [ ] [OK]
Common Mistakes:
  • Using && or || inside [ ]
  • Forgetting spaces around operators
  • Assuming -a is invalid inside [ ]
5. You want to write a bash condition that checks if a variable var is NOT empty AND file1.txt exists OR file2.txt is readable. Which condition is correct?
hard
A. [ -n "$var" -a ( -e file1.txt -o -r file2.txt ) ]
B. [ -n "$var" -a -e file1.txt -o -r file2.txt ]
C. [ -z "$var" -a -e file1.txt -o -r file2.txt ]
D. [ -n "$var" -o -e file1.txt -a -r file2.txt ]

Solution

  1. Step 1: Understand the logic to implement

    We want: var NOT empty AND file1 exists OR file2 readable, i.e., (NOT empty AND file1 exists) OR file2 readable.
  2. Step 2: Check each option's logic and syntax

    A: Parentheses for grouping invalid inside [ ], syntax error.
    B: [ -n "$var" -o -e file1.txt -a -r file2.txt ] wrong logic: mixes OR then AND.
    C: [ -z "$var" -a -e file1.txt -o -r file2.txt ] uses -z (empty), wrong for NOT empty.
    D: [ -n "$var" -a -e file1.txt -o -r file2.txt ] correct: left-to-right ((non-empty -a exists) -o readable).
  3. Final Answer:

    [ -n "$var" -a -e file1.txt -o -r file2.txt ] -> Option B
  4. Quick Check:

    -a/-o left-to-right no grouping needed [OK]
Hint: -a/-o associate left-to-right inside [ ] [OK]
Common Mistakes:
  • Trying to group with ( ) invalid in [ ]
  • Mixing -o and -a without understanding associativity
  • Using -z for non-empty check
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