Automation, Orchestration, and Scripting
In Bash, the test [[ $input =~ "^[0-9]+$" ]] fails even when input is 123. Why?
Show the answer
Correct answerA. The quoted pattern is matched as a literal string, not as a regular expression
AWhy this is right: Quoting the right side of =~ makes Bash match it literally, so ^, + and $ lose their meaning.
BWhy not this: Single quotes also make the pattern literal; the pattern must be unquoted or held in a variable.
CWhy not this: =~ works only inside [[ ]]; single brackets do not support it.
DWhy not this: =~ uses POSIX extended regular expressions, which support the + quantifier.
Since Bash 3.2, any quoted part of the pattern on the right of =~ is matched literally, so regex operators such as ^, $ and * lose their special meaning. Regex patterns must therefore be left unquoted or held in a variable.