📖 Scenario: You are writing a bash script that needs to check multiple conditions on strings and files. The basic test command [ ] has limitations, so you want to use the extended test [[ ]] which supports pattern matching, regex, and logical operators without quoting issues.
🎯 Goal: Build a bash script that uses [[ ]] extended test for string comparison, pattern matching, and combining conditions with logical operators.
📋 What You'll Learn
Use [[ ]] for string comparison
Use [[ ]] with pattern matching using == and wildcards
Use [[ ]] with regex matching using =~
Combine multiple conditions using && and || inside [[ ]]
💡 Why This Matters
🌍 Real World
Extended tests are used in production bash scripts to safely check strings, validate input formats, and combine conditions without worrying about word splitting or globbing issues.
💼 Career
Understanding <code>[[ ]]</code> vs <code>[ ]</code> is a common interview question and essential for writing robust shell scripts in DevOps and system administration roles.
Progress0 / 4 steps
1
Basic string comparison with extended test
Create a variable called lang set to bash. Use [[ ]] to check if lang equals bash and print Match found if true.
Bash Scripting
Hint
Use [[ "$lang" == "bash" ]] for string comparison inside extended test.
2
Pattern matching with wildcards
Create a variable called filename set to report.txt. Use [[ ]] with a wildcard pattern to check if filename ends with .txt and print Text file detected if true.
Bash Scripting
Hint
Use [[ "$filename" == *.txt ]] — the wildcard must not be quoted on the right side.
3
Regex matching with =~
Create a variable called email set to user@example.com. Use [[ ]] with the =~ operator to check if email contains an @ symbol followed by characters and a dot. Print Valid email format if the regex matches.
Bash Scripting
Hint
Use [[ "$email" =~ @.+\..+ ]] — the regex must not be quoted.
4
Combining conditions with && and ||
Create two variables: age set to 25 and role set to admin. Use a single [[ ]] with && to check if age is greater than or equal to 18 AND role equals admin. Print Access granted if both conditions are true.
Bash Scripting
Hint
Use [[ $age -ge 18 && "$role" == "admin" ]] to combine conditions inside a single extended test.
Practice
(1/5)
1. What does the [[ ]] construct do in a bash script?
easy
A. It tests conditions like comparing strings or numbers.
B. It defines a function in bash.
C. It creates a new variable.
D. It runs a command in the background.
Solution
Step 1: Understand the purpose of [[ ]]
The [[ ]] syntax is used in bash to test conditions such as string comparison, number comparison, and file checks.
Step 2: Compare with other options
Options B, C, and D describe other bash features unrelated to [[ ]].
Final Answer:
It tests conditions like comparing strings or numbers. -> Option A
Quick Check:
[[ ]] = condition test [OK]
Hint: Remember: [[ ]] is for testing conditions in bash [OK]
Common Mistakes:
Confusing [[ ]] with function definition syntax
Thinking [[ ]] creates variables
Assuming [[ ]] runs commands
2. Which of the following is the correct syntax to check if variable var equals "hello" using [[ ]]?
easy
A. [[ $var != "hello" ]]
B. [[ $var == "hello" ]]
C. [[ $var === "hello" ]]
D. [[ $var -eq "hello" ]]
Solution
Step 1: Identify correct string comparison operator in [[ ]]
In bash, inside [[ ]], the operator for string equality is ==. -eq is for numeric equality. === is invalid.