0
[myuser@mycomputer]$ word="hello"
[myuser@mycomputer]$ if [[ $word =~ "^hello$" ]]; then echo "it was a hello"; else echo "must have been a goodbye"; fi
must have been a goodbye

我不明白我的错误在哪里,但我期待相反的结果。

4

1 回答 1

2

首先,您必须取消引用您的变量,使用$.

if [[ $word =~ "^hello$" ]]
      ^

然后,您不能将字符串括在双引号"中才能使用正则表达式。

if [[ $word =~ ^hello$ ]]
于 2013-05-16T10:47:24.557 回答