The test command checks conditions like file existence or string comparison and returns true or false based on the result.
Click to reveal answer
beginner
How is the [ ] syntax related to the test command?
The [ ] syntax is a shorthand for the test command. For example, [ -f file ] is the same as test -f file.
Click to reveal answer
beginner
What is the correct way to check if a file exists using [ ]?
Use [ -e filename ]. It returns true if the file exists, false otherwise.
Click to reveal answer
intermediate
Why must there be spaces around the brackets in [ ]?
Because [ and ] are separate commands and arguments. Without spaces, bash treats it as one word and errors occur.
Click to reveal answer
beginner
How do you check if two strings are equal using test or [ ]?
Use [ "$str1" = "$str2" ] or test "$str1" = "$str2". Quotes prevent errors if strings are empty or contain spaces.
Click to reveal answer
What does [ -d /home/user ] check?
AIf /home/user is a file
BIf /home/user is empty
CIf /home/user is a directory
DIf /home/user is writable
✗ Incorrect
-d checks if the path is a directory.
Which is the correct way to check if variable var is empty?
ABoth A and D
B[ -n "$var" ]
C[ -z "$var" ]
D[ "$var" = "" ]
✗ Incorrect
-z checks if string length is zero. Comparing to empty string also works.
What happens if you write [ -f file] without space before ]?
AIt works fine
BSyntax error occurs
CIt checks if file is executable
DIt checks if file is a directory
✗ Incorrect
Missing space before ] causes bash to misinterpret the command and throw a syntax error.
Which command is equivalent to [ "$a" = "$b" ]?
Atest "$a" = "$b"
Btest $a == $b
Ctest $a = $b
Dtest "$a" != "$b"
✗ Incorrect
Quotes are needed to handle empty or spaced strings. Single equals = is used for string comparison.
What does test -e filename check?
AIf filename is empty
BIf filename is executable
CIf filename is a directory
DIf filename exists
✗ Incorrect
-e checks if the file or directory exists.
Explain how to use the test command and [ ] syntax to check if a file exists and is a directory.
Think about the options for file tests and how [ ] is a shortcut for test.
You got /7 concepts.
Describe common mistakes when using [ ] in bash scripts and how to avoid them.
Focus on spacing and quoting to prevent errors.
You got /5 concepts.
Practice
(1/5)
1. What does the [ -f filename ] test check in a bash script?
easy
A. If filename is a directory
B. If filename is empty
C. If filename is executable
D. If filename exists and is a regular file
Solution
Step 1: Understand the -f test
The -f option in [ ] checks if the file exists and is a regular file (not a directory or special file).
Step 2: Compare with other options
-d checks for directory, -x for executable, and -s for non-empty file, so they don't match.
Final Answer:
If filename exists and is a regular file -> Option D
Quick Check:
-f means regular file check [OK]
Hint: Remember: -f means regular file exists [OK]
Common Mistakes:
Confusing -f with -d for directories
Forgetting spaces inside [ ]
Using filename without quotes causing errors
2. Which of the following is the correct syntax to test if variable var is equal to string hello in bash?
easy
A. [ $var = hello ]
B. [ $var == hello ]
C. [ "$var" = "hello" ]
D. [ "$var" == hello ]
Solution
Step 1: Check quoting rules
Variables should be quoted to avoid errors if empty or contain spaces, so "$var" is correct.
Step 2: Check operator correctness
Single equals = is POSIX compliant for string comparison inside [ ]. Double equals == works in [[ ]] but not always in [ ].
Final Answer:
[ "$var" = "hello" ] -> Option C
Quick Check:
Quote variables and use = inside [ ] [OK]
Hint: Always quote variables and use = inside [ ] [OK]
Common Mistakes:
Not quoting variables causing syntax errors
Using == inside [ ] which may fail
Missing spaces around brackets
3. What will be the output of this script snippet?
var=""
if [ -z "$var" ]; then
echo "Empty"
else
echo "Not Empty"
fi
medium
A. Empty
B. Not Empty
C. Syntax Error
D. No output
Solution
Step 1: Understand the -z test
-z returns true if the string length is zero (empty string).
Step 2: Check variable value
Variable var is set to empty string "", so [ -z "$var" ] is true and the if branch runs.
Final Answer:
Empty -> Option A
Quick Check:
-z tests empty string [OK]
Hint: Use -z to check empty strings [OK]
Common Mistakes:
Not quoting variable causing errors
Confusing -z with -n
Expecting else branch to run
4. Identify the error in this bash condition:
if [ $num -gt 10 ]; then
echo "Greater"
fi
medium
A. Missing spaces inside [ ]
B. Variable $num should be quoted
C. Wrong operator for numeric comparison
D. Missing then keyword
Solution
Step 1: Check variable usage in test
When using variables in [ ], they should be quoted to avoid errors if empty or unset.
Step 2: Analyze the given code
The code uses $num unquoted. If num is empty, the test becomes invalid syntax.
Final Answer:
Variable $num should be quoted -> Option B
Quick Check:
Always quote variables in [ ] to avoid syntax errors [OK]
Hint: Quote variables inside [ ] to prevent errors [OK]
Common Mistakes:
Not quoting variables causing syntax errors
Confusing numeric and string operators
Forgetting spaces around brackets
5. You want to check if a file data.txt exists and is not empty before processing it in a script. Which condition correctly does this?
hard
A. [ -f data.txt ] && [ -s data.txt ]
B. [ -f data.txt -o -s data.txt ]
C. [ -f data.txt && -s data.txt ]
D. [ -f data.txt ] || [ -s data.txt ]
Solution
Step 1: Understand the tests
-f checks if file exists and is regular file; -s checks if file size is greater than zero (not empty).
Step 2: Choose correct logical operator
To require both conditions, use logical AND. Inside a single [ ], use -a; or chain separate tests with &&. Using && directly inside [ ] causes syntax error.
Step 3: Analyze options
The option with && inside single [ ] has syntax error. Chained tests with && are clear and reliable. Options using -o or || apply OR logic, incorrect.
Final Answer:
[ -f data.txt ] && [ -s data.txt ] -> Option A
Quick Check:
Use && to combine separate [ ] tests [OK]
Hint: Use separate [ ] with && for AND conditions [OK]
Common Mistakes:
Using -o or || which means OR, not AND
Using && inside a single [ ] (syntax error)
Not checking file size with -s
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.