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

发布于 2024-12-27 17:41:24 字数 237 浏览 2 评论 0原文

通常,我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傲鸠 2025-01-03 17:41:24

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

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

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

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

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:

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

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

if [ "$name" = 'Bob' ]; then ...
ㄟ。诗瑗 2025-01-03 17:41:24

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

if 语句执行命令,如果命令成功则执行 then 部分,如果失败则执行 else 部分(如果有)。 (如果命令以状态 ($?) 0 退出,则命令成功;如果以非零状态退出,命令失败。)

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

命令中是

[ "$name" = 'Bob' ]

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

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

命令中使用

grep -q "$text" $file

man [man test 了解更多信息。

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

[ 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.)

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.)

In

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

the command is

[ "$name" = 'Bob' ]

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

In

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

the command is

grep -q "$text" $file

man [ or man test for more information.

FOOTNOTE: Well, the [ command is almost equivalent 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 have to be implemented that way, but a [ without a matching ] would have made a lot of people very very nervous.)

还在原地等你 2025-01-03 17:41:24

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

检查一下:

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

另一方面,您可能没有使用它的该版本,因为 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:

~ $ 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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文