Recall & Review
beginner
What does the
test command do in bash scripting?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?✗ Incorrect
-d checks if the path is a directory.Which is the correct way to check if variable
var is empty?✗ Incorrect
-z checks if string length is zero. Comparing to empty string also works.What happens if you write
[ -f file] without space before ]?✗ Incorrect
Missing space before
] causes bash to misinterpret the command and throw a syntax error.Which command is equivalent to
[ "$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?✗ 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.