Bash if 语句中何时需要方括号?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8934012/
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-09 21:31:37  来源:igfitidea点击:

When are square brackets required in a Bash if statement?

bashif-statementsquare-bracket

提问by Misha Moroshko

Usually, I use square brackets in the if statement:

通常,我在 if 语句中使用方括号:

if [ "$name" = 'Bob' ]; then ...

But, when I check if grepsucceeded I don't use the square brackets:

但是,当我检查是否grep成功时,我不使用方括号:

if grep -q "$text" $file ; then ...

When are the square brackets necessary in the ifstatement?

if语句中什么时候需要方括号?

回答by chepner

The square brackets are a synonym for the testcommand. An ifstatement checks the exit status of a command in order to decide which branch to take. grep -q "$text"is a command, but "$name" = 'Bob'is not--it's just an expression. testis a command, which takes an expression and evaluates it:

方括号是test命令的同义词。一个if语句检查,以决定采取哪一个分支指令的退出状态。grep -q "$text"是一个命令,但"$name" = 'Bob'不是——它只是一个表达式。test是一个命令,它接受一个表达式并计算它:

if test "$name" = 'Bob'; then ...

Since square brackets are a synonym for the testcommand, you can then rewrite it as your original statement:

由于方括号是test命令的同义词,因此您可以将其重写为原始语句:

if [ "$name" = 'Bob' ]; then ...

回答by Keith Thompson

[is actually a command, equivalent (almost, see below) to the testcommand. It's not part of the shell syntax. (Both [and test, depending on the shell, are often built-in commands as well, but that doesn't affect their behavior, except perhaps for performance.)

[实际上是一个命令,相当于(几乎,见下文)test命令。它不是 shell 语法的一部分。([test,取决于shell,通常也是内置命令,但这不会影响它们的行为,除了性能。)

An ifstatement executes a command and executes the thenpart if the command succeeds, or the elsepart (if any) if it fails. (A command succeeds if it exits with a status ($?) of 0, fails if it exits with a non-zero status.)

一个if语句执行的命令和执行then如果命令成功的一部分,或者else一部分(如果有的话),如果它失败。(如果命令以状态 ( $?) 为 0退出,则命令成功,如果以非零状态退出,则命令失败。)

In

if [ "$name" = 'Bob' ]; then ...

the command is

命令是

[ "$name" = 'Bob' ]

(You could execute that same command directly, without the if.)

(您可以直接执行相同的命令,而无需if.)

In

if grep -q "$text" $file ; then ...

the command is

命令是

grep -q "$text" $file

man [or man testfor more information.

man [man test了解更多信息。

FOOTNOTE: Well, the [command is almostequivalent to the testcommand. The difference is that [requires ]as its last argument, and testdoes not -- and in fact doesn't allow it (more precisely, testdoesn't treat a ]argument specially; for example it could be a valid file name). (It didn't haveto be implemented that way, but a [without a matching ]would have made a lot of people very very nervous.)

脚注:嗯,[命令几乎等同于test命令。不同之处在于,[需要]作为它的最后一个参数,而test不是——事实上不允许它(更准确地说,test]特别对待一个参数;例如它可能是一个有效的文件名)。(它没有已经被实现这种方式,但一个[不匹配]会作出很多人非常非常紧张。)

回答by cha0site

The best way to think of the [ ... ]syntax, is to consider [to be a program - which it is!

[ ... ]考虑语法的最佳方式是将其视为[一个程序 - 确实如此!

Check this out:

看一下这个:

~ $ ls /usr/bin/\[ 
/usr/bin/[

on the other hand, you're probably not using that version of it since bashalso provides [as a shell built-in.

另一方面,您可能没有使用它的那个版本,因为它bash[作为内置的 shell提供。

Anyway, to answer your question: What ifdoes is run the command you give it and see it the return value is 0or not. You use [to do other, more interesting comparisons such as string comparisons. See man [and man bash.

无论如何,要回答你的问题:什么if是运行你给它的命令,看看它的返回值是0不是。您习惯于[进行其他更有趣的比较,例如字符串比较。见man [man bash