Bird
0
0

Which of the following is the correct syntax to check if file file.txt exists AND is readable in bash?

easy📝 Syntax Q12 of 15
Bash Scripting - Conditionals
Which of the following is the correct syntax to check if file file.txt exists AND is readable in bash?
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 ]
Step-by-Step Solution
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]
Quick Trick: Use -a for AND inside [ ], not && or -o for AND [OK]
Common Mistakes:
MISTAKES
  • Using -o instead of -a for AND
  • Using && inside [ ] which causes syntax error
  • Negating conditions incorrectly with !

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes