Bash if 语句中何时需要方括号?
通常,我在 if 语句中使用方括号:
if [ "$name" = 'Bob' ]; then ...
但是,当我检查 grep
是否成功时,我不使用方括号:
if grep -q "$text" $file ; then ...
if
中何时需要方括号> 声明?
Usually, I use square brackets in the if statement:
if [ "$name" = 'Bob' ]; then ...
But, when I check if grep
succeeded I don't use the square brackets:
if grep -q "$text" $file ; then ...
When are the square brackets necessary in the if
statement?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
方括号是
test
命令的同义词。if
语句检查命令的退出状态,以便决定采用哪个分支。grep -q "$text"
是一个命令,但"$name" = 'Bob'
不是 - 它只是一个表达式。test
是一个命令,它接受一个表达式并对其求值:由于方括号是
test
命令的同义词,因此您可以将其重写为原始语句:The square brackets are a synonym for the
test
command. Anif
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:Since square brackets are a synonym for the
test
command, you can then rewrite it as your original statement:[
实际上是一个命令,相当于(几乎,见下文)test
命令。它不是 shell 语法的一部分。 ([
和test
,取决于 shell,通常也是内置命令,但这不会影响它们的行为,除了性能之外。)
if
语句执行命令,如果命令成功则执行then
部分,如果失败则执行else
部分(如果有)。 (如果命令以状态 ($?
) 0 退出,则命令成功;如果以非零状态退出,命令失败。)在
命令中是
(您可以直接执行相同的命令,无需
if
。)在
命令中使用
man [
或man test
了解更多信息。脚注:嗯,
[
命令几乎等同于test
命令。不同之处在于[
需要]
作为其最后一个参数,而test
不需要 - 事实上不允许它(更准确地说) ,test
不会特殊对待]
参数;例如,它可能是有效的文件名)。 (它没有必须以这种方式实现,但是没有匹配]
的[
会让很多人非常紧张.)[
is actually a command, equivalent (almost, see below) to thetest
command. It's not part of the shell syntax. (Both[
andtest
, depending on the shell, are often built-in commands as well, but that doesn't affect their behavior, except perhaps for performance.)An
if
statement executes a command and executes thethen
part if the command succeeds, or theelse
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.)In
the command is
(You could execute that same command directly, without the
if
.)In
the command is
man [
orman test
for more information.FOOTNOTE: Well, the
[
command is almost equivalent to thetest
command. The difference is that[
requires]
as its last argument, andtest
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 have to be implemented that way, but a[
without a matching]
would have made a lot of people very very nervous.)考虑
[ ... ]
语法的最佳方式是将[
视为一个程序 - 确实如此!检查一下:
另一方面,您可能没有使用它的该版本,因为
bash
还提供[
作为内置 shell。无论如何,回答你的问题:
if
所做的是运行你给它的命令,看看它的返回值是0
或不是。您可以使用[
进行其他更有趣的比较,例如字符串比较。请参阅man [
和man bash
。The best way to think of the
[ ... ]
syntax, is to consider[
to be a program - which it is!Check this out:
on the other hand, you're probably not using that version of it since
bash
also provides[
as a shell built-in.Anyway, to answer your question: What
if
does is run the command you give it and see it the return value is0
or not. You use[
to do other, more interesting comparisons such as string comparisons. Seeman [
andman bash
.