bash 如果等于运算符没有被空格包围,为什么它不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4977367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:26:58  来源:igfitidea点击:

Why equal to operator does not work if it is not surrounded by space?

bashshellsyntaxscripting

提问by Andrew-Dufresne

I tried the following script

我尝试了以下脚本

#!/bin/bash
var1="Test 1" 
var2="Test 2"
if [ "$var1"="$var2" ] 
  then 
    echo "Equal" 
  else 
    echo "Not equal"
fi

It gave me Equal. Although it should have printed Not equal

它给了我Equal。虽然它应该打印Not equal

Only when I inserted space around =it worked as intended

只有当我在=它周围插入空间时才按预期工作

if [ "$var1" = "$var2" ] 

and printed Not equal

并打印 Not equal

Why is it so? Why "$var1"="$var2"is not same as "$var1" = "$var2"?

为什么会这样?为什么"$var1"="$var2"不一样"$var1" = "$var2"

Moreover, when I wrote if [ "$var1"= "$var2" ], it gave

此外,当我写的时候if [ "$var1"= "$var2" ],它给了

line 4: [: Test 1=: unary operator expected

What does it it mean? How come its expecting unary operator?

这是什么意思?它期待的一元运算符是怎么来的?

回答by CB Bailey

test(or [ expr ]) is a builtin function. Like all functions in bash, you pass it's arguments as whitespace separated words.

test(或[ expr ]) 是一个内置函数。像 bash 中的所有函数一样,您将它的参数作为空格分隔的单词传递。

As the man page for bash builtins states: "Each operator and operand must be a separate argument."

正如 bash 内置程序的手册页所述:“每个运算符和操作数必须是一个单独的参数。”

It's just the way bash and most other Unix shells work.

这就是 bash 和大多数其他 Unix shell 的工作方式。

Variable assignment is different.

变量赋值不同。

In bash a variable assignment has the syntax: name=[value]. You cannot put unquoted spaces around the =because bash would not interpret this as the assignment you intend. bash treats most lists of words as a command with parameters.

在 bash 中,变量赋值的语法为:name=[value]. 您不能在周围放置不带引号的空格,=因为 bash 不会将其解释为您想要的分配。bash 将大多数单词列表视为带有参数的命令。

E.g.

例如

# call the command or function 'abc' with '=def' as argument
abc =def

# call 'def' with the variable 'abc' set to the empty string
abc= def

# call 'ghi' with 'abc' set to 'def'
abc=def ghi

# set 'abc' to 'def ghi'
abc="def ghi"

回答by William Pursell

When the shell reads

当外壳读取

if [ "$var1" = "$var2" ]

it invokes the command [ with 4 arguments. Whether [ is a builtin or an external command is irrelevant, but it may help to understand that it may be the external command /bin/[. The second argument is the literal '=' and the fourth is ']'. However, when the shell reads

它调用带有 4 个参数的命令 [。[ 是内置命令还是外部命令无关紧要,但理解它可能是外部命令 /bin/[ 可能会有所帮助。第二个参数是文字“=”,第四个参数是“]”。但是,当 shell 读取

if [ "$var1"= "$var2" ]

[ only gets 3 arguments: the expansion of $var1 with '=' appended, the expansion of $var2, and ']'. When it gets only 3 arguments, it expects the last argument to be ']' and the first argument to be a unary operator.

[ 只得到 3 个参数:附加了 '=' 的 $var1 的扩展、$var2 的扩展和 ']'。当它只得到 3 个参数时,它期望最后一个参数是 ']',第一个参数是一元运算符。

回答by Jahid

To add to the existing explanation, "$var1"="$var2"is just a single non-empty string, and thus always evaluates as true in a conditional.

添加到现有解释中,"$var1"="$var2"只是一个非空字符串,因此在条件中始终评估为真。

[ "$var1"="$var2" ] && echo true

The above command will always print out true(even if var1and var2be empty).

上面的命令将始终打印出来true(即使var1并且var2为空)。

回答by knesenko

In bash the best is to use [[ ]]:

在 bash 中最好使用 [[ ]]:

x="test"
y="test"
if [[ "${x}" = "${y}" ]]; then
    echo "Equals"
else
    echo "No equals"
fi