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
When are square brackets required in a Bash if statement?
提问by Misha Moroshko
Usually, I use square brackets in the if statement:
通常,我在 if 语句中使用方括号:
if [ "$name" = 'Bob' ]; then ...
But, when I check if grep
succeeded I don't use the square brackets:
但是,当我检查是否grep
成功时,我不使用方括号:
if grep -q "$text" $file ; then ...
When are the square brackets necessary in the if
statement?
if
语句中什么时候需要方括号?
回答by chepner
The square brackets are a synonym for the test
command. An if
statement 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. test
is 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 test
command, 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 test
command. 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 if
statement executes a command and executes the then
part if the command succeeds, or the else
part (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 test
for more information.
man [
或man test
了解更多信息。
FOOTNOTE: Well, the [
command is almostequivalent to the test
command. The difference is that [
requires ]
as its last argument, and test
does not -- and in fact doesn't allow it (more precisely, test
doesn'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 bash
also provides [
as a shell built-in.
另一方面,您可能没有使用它的那个版本,因为它bash
也[
作为内置的 shell提供。
Anyway, to answer your question: What if
does is run the command you give it and see it the return value is 0
or not. You use [
to do other, more interesting comparisons such as string comparisons. See man [
and man bash
.
无论如何,要回答你的问题:什么if
是运行你给它的命令,看看它的返回值是0
不是。您习惯于[
进行其他更有趣的比较,例如字符串比较。见man [
和man bash
。