技术解析

bash 的提问。
0
2021-07-12 00:47:35
idczone
大家好,小弟不才,最近在看一本《linux就是这个范儿》的书籍,里面作者卖了一个关子,想问下这答案是什么,昨天想了一夜,也没想出答案。
为什么没有“]”这个命令程序呢?

就像你说的[test_condition],中的"["是一个命令,而不是语法中的括号。如果是括号就一定要成对出现了。
之所以要有"]",我认为是为了语义上符合逻辑。

我认为[是个命令,那]可以是参数,形成闭合。
如果]是命令,那没法搞,因为没有开头是 ] xxx 的语法传统。它的参数按照传统的命令行参数传递顺利,传不进去。

2 楼是正确的
[ a ]
就是执行命令 [ 带两个参数 a 和 ]
[ 命令要求最后一个参数必须是 ] 否则报错
它最终等价于 test a
[ 和 test 是完全一样的命令,除了对最后一个参数的解释的差异

下面是我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.

数据地带为您的网站提供全球顶级IDC资源
在线咨询
专属客服