0
0
Bash Scriptingscripting~15 mins

[[ ]] extended test in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Extended test
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use [[ $age -ge 18 && "$role" == "admin" ]] to combine conditions inside a single extended test.