bash 的提问。

2015-06-16 09:40:53 +08:00
 webjin
大家好,小弟不才,最近在看一本《linux就是这个范儿》的书籍,里面作者卖了一个关子,想问下这答案是什么,昨天想了一夜,也没想出答案。
为什么没有“]”这个命令程序呢?
1943 次点击
所在节点    Linux
4 条回复
TerrenceSun
2015-06-16 10:19:25 +08:00
就像你说的[test_condition],中的"["是一个命令,而不是语法中的括号。如果是括号就一定要成对出现了。
之所以要有"]",我认为是为了语义上符合逻辑。
lerosua
2015-06-16 11:21:30 +08:00
我认为[是个命令,那]可以是参数,形成闭合。
如果]是命令,那没法搞,因为没有开头是 ] xxx 的语法传统。它的参数按照传统的命令行参数传递顺利,传不进去。
SoloCompany
2015-06-16 14:05:23 +08:00
2 楼是正确的

[ a ]

就是执行命令 [ 带两个参数 a 和 ]
[ 命令要求最后一个参数必须是 ] 否则报错

它最终等价于 test a

[ 和 test 是完全一样的命令,除了对最后一个参数的解释的差异
webjin
2015-06-16 15:36:00 +08:00
下面是我man test 得到的解说,但是我英语不才,翻译看不懂专业术语
=====================================================
Except for -h and -L, all FILE-related tests dereference symbolic links. Beware that parentheses need to be escaped (e.g., by
backslashes) for shells. INTEGER may also be -l STRING, which evaluates to the length of STRING.

NOTE: [ honors the --help and --version options, but test does not. test treats each of those as it treats any other nonempty
STRING.

NOTE: your shell may have its own version of test and/or [, which usually supersedes the version described here. Please refer
to your shell’s documentation for details about the options it supports.

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/198865

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX