语法错误在= =运算符附近

use*_*479 1 regex bash shell conditional-operator operator-keyword

当我运行这个脚本时:

#!/bin/bash

if [[ "abcd" =~ ^.*$ ]]; then
    echo "something"
fi
Run Code Online (Sandbox Code Playgroud)

我明白了:

./tmp2.sh: line 3: conditional binary operator expected
./tmp2.sh: line 3: syntax error near `=~'
./tmp2.sh: line 3: `if [[ "abcd" =~ ^.*$ ]]; then'
Run Code Online (Sandbox Code Playgroud)

我已经尝试了我找到的所有建议,但仍然是一样的:/请帮助我!

mkl*_*nt0 6

假设你看到一个特定的bash错误消息,我们可以排除一些其他bash正在运行的脚本(如果它是一个POSIX的功能,只有外壳,如sh在某些系统上,你会看到,而不是一个错误有关的消息[[).

最可能的解释是:

  • 您的bash版本是< 3.0,因此不支持=~运营商.
    • 要验证,请echo $BASH_VERSION在脚本中运行.

你看到的具体错误是bash的说法:"我看到一个字符串,我不承认它是一个操作员."