Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

3
  • [[ $a =~ a|b ]] works with | being interpreted as OR in regex. In the approach of using a variable, the same regex is assigned to the variable regexp='a|b'. So the example doesn't seem to show that the variable approach avoids problems with quoting characters. However, the variable approach does make a difference when =~ for regex is replaced with = for globbing. Commented Jul 27, 2017 at 21:25
  • [[ $a = a|b ]] results in syntax error in conditional expression: unexpected token '|', while regexp='a|b'; [[ $a = $regexp ]] doesn't. Why is the difference? Does parameter expansion of regexp delay a|b's appearing in the conditional expression, so that the delay can avoid some interpretation step which reports error on [[ $a = a|b ]]? What interpretation step is that? Commented Jul 27, 2017 at 21:31
  • Your $regexp should *not* be quoted. line is worth its weight in gold. Thanks! Commented Oct 19, 2018 at 13:54