5

This question is about the proper way to write regular expression literals in a match expression under bash.

In zsh, the match below succeeds, as I expected:

% [[ ' 123 ' =~ '^ [0-9]+ $' ]]; echo $?
0

Not so in bash:

$ [[ ' 123 ' =~ '^ [0-9]+ $' ]]; echo $?
1

I know that this match succeeds in bash

$ ( regexp='^ [0-9]+ $'; [[ ' 123 ' =~ $regexp ]]; echo $? )
0

...but it requires assigning the regular expression to an intermediate variable.


My question is: How does one write an arbitrary regular expression literal in a match expression under bash?

8

It's best to put it in a variable, as recommended by the Bash reference manual 3.2.4.2 Conditional Constructs:

Storing the regular expression in a shell variable is often a useful way to avoid problems with quoting characters that are special to the shell. It is sometimes difficult to specify a regular expression literally without using quotes, or to keep track of the quoting used by regular expressions while paying attention to the shell’s quote removal. Using a shell variable to store the pattern decreases these problems.

However in order to write it directly inside of the bash extended test you need to remove the quotes and escape the spaces:

$ [[ ' 123 ' =~ ^\ [0-9]+\ $ ]]; echo $?
0
3

In bash, the canonical answer is: use a var

$ reg='^ [0-9]+ $'
$ [[ ' 123 ' =~ $reg ]]; echo $?

That works for spaces, backslash, and many other things:

$ reg='^ 12\3 $'
$ [[ ' 12\3 ' =~ $reg ]]; echo $?
0

If you want to write it as a literal you need to play (carefully) with quoting.
You need to quote the parts that need to be literal and leave the parts that should be interpreted as a regex un-quoted. The space needs quoting. Here using backslah:

$ [[ ' 123 ' =~ ^\ [0-9]+\ $ ]]; echo $?
0

And here using double quotes:

$ [[ ' 123 ' =~ ^" "[0-9]+" "$ ]]; echo $?

But that "quoting" gets real messy with things like a backslash:

$ [[ ' 12\3 ' =~ ^" "[0-9]+"\\"[0-9]+" "$ ]]; echo $?
0

Simpler, safer:

$ reg='^ [0-9]+\\[0-9]+ $'
$ [[ ' 12\3 ' =~ $reg ]]; echo $?
0

And, no, you don't need a subshell to do that (the parenthesis in your question).

$ reg='^ [0-9]+\\[0-9]+ $'; [[ ' 12\3 ' =~ $reg ]]; echo $?

Yes, that may seem annoying. And, yes, the command you presented happens to work in zsh:

$ [[ ' 123 ' =~ '^ [0-9]+ $' ]]; echo $?
0

But quoting is always a problem (in any shell), what should happen to backslash ?:

% [[ ' 12\3 ' =~ ' 12\3 ' ]]; echo $?
zsh: failed to compile regex: Invalid back reference
1


% [[ ' 12\3 ' =~ '^ [0-9]+\\3 $' ]]; echo $?
0

Double it !. It is not exactly: literal.

  • I know that the subshell is not needed for the match; I use subshells to avoid polluting my current shell's namespace with a variable that I need to use only once. – kjo Nov 21 '18 at 21:08
  • An unset regexp is always faster than a subshell. @kjo – Isaac Nov 21 '18 at 23:12
  • Maybe, but I don't care about the few milliseconds here and there. The subshell takes fewer keystrokes, it is robust to any number of changes I may do in the subshell (not just the setting of one variable), and there's no chance I will forget to run unset afterwards. – kjo Nov 22 '18 at 16:54
  • Sure, why fight the waste if it is easier?. Why reduce the use of plastics until we get a plastic planet? @kjo – Isaac Nov 23 '18 at 0:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.