为什么在Bash中[[和]]周围应该有空格? [英] Why should there be spaces around '[' and ']' in 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屋!