0
0
Bash Scriptingscripting~5 mins

[[ ]] extended test in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the [[ ]] syntax represent in bash scripting?
It is an extended test command used for conditional expressions, offering more features and safer syntax than the older [ ] test.
Click to reveal answer
intermediate
How does [[ ]] differ from [ ] in bash?
The [[ ]] supports pattern matching with ==, allows && and || inside without escaping, and prevents word splitting and pathname expansion.
Click to reveal answer
beginner
What is the result of this command: [[ "abc" == a* ]] && echo yes || echo no
It prints 'yes' because [[ ]] supports pattern matching, and 'abc' matches the pattern 'a*'.
Click to reveal answer
intermediate
Can you use regex matching inside [[ ]]? If yes, how?
Yes, using the =~ operator, e.g., [[ $var =~ ^[0-9]+$ ]] tests if $var contains only digits.
Click to reveal answer
intermediate
Why is [[ ]] considered safer than [ ] when testing variables?
Because [[ ]] does not perform word splitting or pathname expansion, it avoids errors when variables contain spaces or special characters.
Click to reveal answer
Which of these is a valid use of [[ ]] in bash?
A[ $name = "John" ]
B[ $name == "John" ]
C[[ $name = "John" ]]
D[[ $name == "John" ]]
What does this command print? [[ 5 -gt 3 && 2 -lt 4 ]] && echo yes || echo no
ANothing
Byes
CSyntax error
Dno
Which operator is used inside [[ ]] for regex matching?
A==
B-eq
C=~
D!=
What happens if a variable with spaces is tested using [ ] without quotes?
AIt causes word splitting and errors
BIt automatically quotes the variable
CIt works fine
DIt ignores spaces
Which is true about [[ ]] in bash?
AIt is a shell keyword, not a command
BIt is an external program
CIt behaves exactly like [ ]
DIt cannot be used with variables
Explain the advantages of using [[ ]] over [ ] in bash scripting.
Think about safety and extra features in condition tests.
You got /4 concepts.
    Describe how to use regex matching inside [[ ]] and give an example.
    Look for the operator that tests patterns, not just equality.
    You got /3 concepts.