V.Collette V.Collette - 1 year ago 104
Bash Question

Regexp in a variable Shell

I'm doing some regexp in shell to find coding style error in *.c files.
Actually I'm doing something like this:

# Operator <
if [[ "$1" =~ ([^ ]<|<[^ =$]) ]]; then
warn "$wmsg_space_operator (operator: <)"

But I want to do it like this:

# Operator <
regexpOp=([^ ]<|<[^ =$])
if [[ "$1" =~ $regexpOp ]]; then
warn "$wmsg_space_operator (operator: <)"

How can I do?

Answer Source

Put regexpOp in quotes

regexpOp='([^ ]<|<[^ =$])'

and rest is assured


Your regex string contain spaces and anything after the space will be counted as another command.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download