为什么这个简单的bash代码会出现语法错误?

Tim*_*tin 4 bash syntax-error

我有以下bash代码,它是从"bash cookbook"(第1版)复制并粘贴的:

#!/bin/bash

VERBOSE=0;
if [[ $1 =-v ]]
then
    VERBOSE=1;
    shift;
fi
Run Code Online (Sandbox Code Playgroud)

当我运行它(bash 4.0.33)时,我得到以下语法错误:

./test.sh: line 4: conditional binary operator expected
./test.sh: line 4: syntax error near `=-v'
./test.sh: line 4: `if [[ $1 =-v ]]'
Run Code Online (Sandbox Code Playgroud)

这是否与bash cookbook中的错误打印一样简单,或者是否存在版本不兼容或其他内容?最明显的解决方案是什么?我尝试过改变运算符的各种组合,但我并不熟悉bash脚本.

小智 11

Bash使用空格来标记脚本.这条线:

if [[ $1 =-v ]]
Run Code Online (Sandbox Code Playgroud)

应该:

if [[ $1 = -v ]]
Run Code Online (Sandbox Code Playgroud)