这发生在字符t和值根。相当困惑
- $ echo [s]
- [s]
- $ echo [t]
- t
- $ echo [ t ]
- [ t ]
- $ echo [root]
- t
不是一个壳的习惯(并不愿意成为)我发现令人惊讶的如何文件名扩展被设计为行为时,没有找到匹配。我会报
Bash reference
Bash scans each word for the characters
@H_404_19@*
,?
,and[
. If one of
these characters appears,then the word is regarded as a pattern,and
replaced with an alphabetically sorted list of file names matching the
pattern. If no matching file names are found:if the shell option nullglob
is disabled,the word is left unchangedif the shell option nullglob
is set the word is removedIf the failglob
shell option is set,an error message is printed and the command is not executed
好消息是这件事是可配置的。坏的一个是一个脚本可以失败在许多方式,一个人不期望 – 至少,我没有,我花了一些时间来了解为什么echo行为的方式,你发布,只是发现,这是因为一个组合的古怪的文件名(谁想要命名文件t?),隐藏配置(nullglob禁用,默认选项但仍隐藏)和一个无害的命令。
我说无害,因为这是你得到的,例如,当目标是ls(失败,因为没有找到文件):
- raffaele@Aldebaran:~$ mkdir test
- raffaele@Aldebaran:~$ cd test
- raffaele@Aldebaran:~/test$ touch t
- raffaele@Aldebaran:~/test$ ls [t]
- t
- raffaele@Aldebaran:~/test$ ls [v]
- ls: cannot access [v]: No such file or directory