微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么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

原文地址:https://www.jb51.cc/bash/391152.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐