Regexp在变量Shell中

时间:2016-08-11 13:23:03

标签: regex bash shell

我在shell中执行一些regexp来查找* .c文件中的编码样式错误。 其实我做的是这样的事情:

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

但我想这样做:

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

我该怎么办?

2 个答案:

答案 0 :(得分:1)

您需要引用作业,特别是空白。

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

答案 1 :(得分:1)

regexpOp放在引号

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

保证休息

<强>为什么吗

你的正则表达式字符串包含空格,空格后的任何内容都将被视为另一个命令。

相关问题