为什么bash“echo [t]”导致“t”而不是“[t]”

这发生在字符t和值根。相当困惑
$ echo [s]
[s]
$ echo [t]
t
$ echo [ t ]
[ t ]
$ echo [root]
t
不是一个壳的习惯(并不愿意成为)我发现令人惊讶的如何文件名扩展被设计为行为时,没有找到匹配。我会报 Bash reference

Bash scans each word for the characters *,?,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 unchanged
  • if the shell option nullglob is set the word is removed
  • If 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

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...