且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-13 15:55:52

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

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

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

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

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

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

变量赋值不同.

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

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.

例如

# 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"