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.