加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-15 16:26:13 所属栏目:安全 来源:网络整理
导读:这发生在字符t和值根。相当困惑 $ echo [s][s]$ echo [t]t$ echo [ t ][ t ]$ echo [root]t 不是一个壳的习惯(并不愿意成为)我发现令人惊讶的如何文件名扩展被设计为行为时,没有找到匹配。我会报 Bash reference Bash scans each word for the characters *
这发生在字符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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读