There is not a simple answer to this question at this site yet. This question aims to give a simple and clear answer.

It is usually recommended to use a variable ($regex) in this construct:

if [[ $var =~ $regex ]]; then …

Why is that so?

merged by Michael Mrozek Sep 19 '18 at 20:42

This question was merged with How does storing the regular expression in a shell variable avoid problems with quoting characters that are special to the shell? because it is an exact duplicate of that question.

  • @Isaac I'm having trouble seeing how this isn't a duplicate of the post Stéphane Chazelas linked – Michael Mrozek Sep 19 '18 at 19:30
  • @MichaelMrozek IMhO!!: that post is complex, this aims to be simpler, more general. This post already has more than 1000 views (in 3 months), the other still has less than 700 (after a year). It was a message of this reaching 1000 views that prompted me to look back into it. Maybe you could merge both. Maybe keep the title and question of this post and place both answers together. It is your call ... if you still see a more visited question (more relevant to people searching for solutions) as the duplicate then mark it as such. If so, I probably should just erase it, no longer helpful. – Isaac Sep 19 '18 at 20:25
  • Sounds like a good plan; will merge your answer into the other post and keep this question as a duplicate pointer since more people seem to be finding this one when they search for the issue – Michael Mrozek Sep 19 '18 at 20:41