bash if 语句有问题 [Error==Error]: not found

pyl*_*ler 1 bash

每次我运行我的脚本时,下面的 if 语句都会给我错误;

script.sh: [Error==Error]: not found
Run Code Online (Sandbox Code Playgroud)

或者

script.sh: [Error==-2]: not found
Run Code Online (Sandbox Code Playgroud)

if ["$P1"=="$P2"];then
            echo $name
fi
Run Code Online (Sandbox Code Playgroud)

我试过其他版本

    if ["$P1"=="$P2"]
            then
            echo $name
    fi
Run Code Online (Sandbox Code Playgroud)

    if [[ "$P1" == "$P2" ]]
            then
            echo $name
fi

P1="Error"
P2="$(sed -n '1p' somefile.txt)"
Run Code Online (Sandbox Code Playgroud)

somefile.txt 可能包含数字或字符串

Joh*_*024 7

空格很重要。用:

if [ "$P1" = "$P2" ]
Run Code Online (Sandbox Code Playgroud)

什么地方出了错

当 shell 看到 时["$P1"=="$P2"],它会将其解释为单个单词并查找与该单词匹配的命令。由于不存在这样的命令,您会收到not found错误消息。