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

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

问题描述

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

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 ];

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

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!

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

help [

尽管它们确实完全相同,但test却具有更详细的帮助页面.检查

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

help test

...了解更多信息.

...for more information.

此外,请注意,我故意使用的是help test而不是man test.这是因为test[是当今的shell内置命令.它们的功能集可能与man页面中描述的命令的coreutils的/bin/test/bin/[不同.

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天全站免登陆