[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
我不明白我的错误在哪里,但我期待相反的结果。
答案 0 :(得分:2)
首先,您必须使用$
取消引用您的变量。
if [[ $word =~ "^hello$" ]]
^
然后,您不能将字符串括在双引号"
中以便能够使用正则表达式。
if [[ $word =~ ^hello$ ]]