为什么要周围有空格' ['和']'在狂欢中?
我试图编写一个使用语句的bash脚本。
if[$CHOICE -eq 1];
该脚本给我错误,直到我在之前和之后给出一个空间[
和
if [ $CHOICE -eq 1 ];
]在狂欢中很重要?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一旦您掌握了
[
是 命令 ,一个批次就变得更加清晰了![
是拼写“test
”的另一种方法。但是,尽管它们完全相同
test
事实证明有一个更详细的帮助页面。检查...以获取更多信息。
此外,请注意,我故意使用
帮助测试
而不是MAN TEST
。那是因为test
和[
是如今的壳命令。他们的功能集可能不同于/bin/test
和/bin/[
的coreutils,这是man
页面中所述的命令。Once you grasp that
[
is a command, a whole lot becomes clearer![
is another way to spell "test
".However while they do exactly the same,
test
turns out to have a more detailed help page. Check...for more information.
Furthermore note that I'm using, by intention,
help test
and notman test
. That's becausetest
and[
are shell builtin commands nowadays. Their feature set might differ from/bin/test
and/bin/[
from coreutils which are the commands described in theman
pages.来自另一个问题:
From another question:
[
是命令,$ opeach
应该是一个参数,但是通过执行[$ opeach>
(之间没有任何空间[和
$选择
)您正在尝试运行一个名为的命令[$选择
。命令的语法是:[
is a command and$CHOICE
should be an argument, but by doing[$CHOICE
(without any space between[
and$CHOICE
) you are trying to run a command named[$CHOICE
. The syntax for command is:[
是test
命令。因此需要空间。[
is atest
command. So it requires space.值得注意的是,[也用于地球匹配,这可能会使您陷入困境。
It's worth noting that [ is also used in glob matching, which can get you into trouble.