为什么 Bash 中的 '[' 和 ']' 周围应该有空格? [英] Why should there be spaces around '[' and ']' in Bash?

查看:23
本文介绍了为什么 Bash 中的 '[' 和 ']' 周围应该有空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个使用 if 语句的 Bash 脚本.

if[$CHOICE -eq 1];

脚本一直给我错误,直到我在 [ 前后和 ] 之前和之后给了一个空格,如下所示:

if [ $CHOICE -eq 1 ];

我的问题是,为什么 Bash 中方括号周围的空格如此重要?

解决方案

一旦你明白[ 是一个命令,一切变得更清晰!

[ 是另一种拼写test"的方式.

help [

然而,虽然他们完全做同样的事情,test 却有一个更详细的帮助页面.检查

帮助测试

...了解更多信息.

<小时>

另外请注意,我有意使用 help test 而不是 man test.那是因为 test[ 现在是 shell 内置命令.它们的功能集可能与 coreutils 中的 /bin/test/bin/[ 不同,它们是 man 页面中描述的命令.>

I was trying to write a Bash script that uses an if statement.

if[$CHOICE -eq 1];

The script was giving me errors until I gave a space before and after [ and before ] as shown below:

if [ $CHOICE -eq 1 ];

My question here is, why is the space around the square brackets so important in Bash?

解决方案

Once you grasp that [ is a command, a whole lot becomes clearer!

[ is another way to spell "test".

help [

However while they do exactly the same, test turns out to have a more detailed help page. Check

help test

...for more information.


Furthermore note that I'm using, by intention, help test and not man test. That's because test and [ are shell builtin commands nowadays. Their feature set might differ from /bin/test and /bin/[ from coreutils which are the commands described in the man pages.

这篇关于为什么 Bash 中的 '[' 和 ']' 周围应该有空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆