Discover how a tiny symbol =~ can save you hours of tedious file checks!
Why Regex in [[ ]] with =~ in Bash Scripting? - Purpose & Use Cases
Imagine you have a list of filenames and you want to find which ones end with .txt or .log by checking each name manually.
You open each file, look at its name, and try to remember the pattern you want. This takes a lot of time and you might miss some files.
Manually checking each filename is slow and boring. You can easily make mistakes by forgetting a pattern or mixing up file extensions.
Also, if you want to check more complex patterns, it becomes almost impossible without automation.
Using regex inside the [[ ]] test with =~ lets you quickly check if a string matches a pattern.
This means you can write a simple script that automatically finds all files matching your criteria without opening or reading them one by one.
if [[ "$filename" == *.txt ]] || [[ "$filename" == *.log ]]; then echo "Match" fi
if [[ "$filename" =~ \.(txt|log)$ ]]; then echo "Match" fi
You can easily and accurately check complex text patterns in scripts, making automation smarter and faster.
When cleaning up a folder, you can quickly find all image files like .jpg, .png, or .gif using one regex pattern instead of multiple checks.
Manual pattern checks are slow and error-prone.
Regex with =~ inside [[ ]] simplifies pattern matching.
This makes scripts more powerful and easier to write.
Practice
[[ "hello123" =~ [a-z]+[0-9]+ ]] check in a bash script?Solution
Step 1: Understand the regex pattern
The pattern[a-z]+[0-9]+means one or more lowercase letters followed by one or more digits.Step 2: Apply the pattern to the string
The string "hello123" starts with letters "hello" and ends with digits "123", so it matches the pattern.Final Answer:
If the string contains letters followed by numbers -> Option BQuick Check:
Regex matches letters then digits = A [OK]
- Thinking quotes are needed around regex inside [[ ]]
- Confusing exact match with pattern match
- Assuming regex matches anywhere without order
var matches regex ^[0-9]+$ in bash?Solution
Step 1: Recall regex syntax in [[ ]]
Inside [[ ]], the regex pattern should NOT be quoted to work correctly.Step 2: Check each option
[[ $var =~ ^[0-9]+$ ]] uses[[ $var =~ ^[0-9]+$ ]]without quotes around regex, which is correct syntax.Final Answer:
[[ $var =~ ^[0-9]+$ ]] -> Option CQuick Check:
No quotes around regex pattern = B [OK]
- Putting quotes around regex pattern causing syntax errors
- Quoting variable but not regex or vice versa
- Using single brackets [ ] instead of [[ ]]
text="abc123" if [[ $text =~ ^[a-z]+[0-9]+$ ]]; then echo "Match" else echo "No match" fi
Solution
Step 1: Analyze the regex pattern
The pattern^[a-z]+[0-9]+$means the string must start with one or more lowercase letters and end with one or more digits, with nothing else.Step 2: Check the string against the pattern
The string "abc123" starts with letters "abc" and ends with digits "123", so it matches the pattern exactly.Final Answer:
Match -> Option AQuick Check:
String matches regex pattern = D [OK]
- Ignoring anchors ^ and $ in regex
- Assuming partial match is enough
- Quoting regex pattern causing failure
input="2023-04-01"
if [[ "$input" =~ "^[0-9]{4}-[0-9]{2}-[0-9]{2}$" ]]; then
echo "Valid date"
else
echo "Invalid date"
fiSolution
Step 1: Check quoting rules for regex in [[ ]]
Regex pattern inside [[ ]] with =~ must NOT be quoted; quotes make it a string literal, breaking regex matching.Step 2: Identify the error in the snippet
The snippet quotes the regex pattern, causing the condition to always fail and print "Invalid date".Final Answer:
Quotes around regex cause the match to fail -> Option AQuick Check:
No quotes around regex pattern = C [OK]
- Quoting regex pattern inside [[ ]]
- Forgetting to quote variable when needed
- Assuming regex syntax errors without checking quotes
filename ends with either ".jpg" or ".png" using regex in bash. Which condition correctly tests this?Solution
Step 1: Understand regex for file extensions
The pattern\.(jpg|png)$matches a dot followed by 'jpg' or 'png' at the end of the string.Step 2: Apply correct quoting rules
Regex pattern must NOT be quoted inside [[ ]], and variable can be unquoted if it has no spaces. [[ $filename =~ \.(jpg|png)$ ]] correctly uses unquoted regex and variable.Final Answer:
[[ $filename =~ \.(jpg|png)$ ]] -> Option DQuick Check:
Unquoted regex with alternation = A [OK]
- Quoting regex pattern causing failure
- Not escaping dot before extension
- Quoting variable unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
