1

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: <)"
fi

But I want to do it like this:

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

How can I do?

2 Answers 2

1

Put regexpOp in quotes

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

and rest is assured

Why?

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

Sign up to request clarification or add additional context in comments.

Comments

1

You need to quote the assignment, specifically for the whitespace.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.