当目录存在与否时,bash shell给出奇怪的结果 [英] bash shell gives weird result when directory exists vs not

查看:69
本文介绍了当目录存在与否时,bash shell给出奇怪的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我浪费了整整一天的时间来编写一个表现很奇怪的脚本.我发现了我无法解释的内容.

Alright, I've wasted a full day on a script that was behaving very oddly. I have found out something that I can't explain.

ny-02-ops:~$ rm -fr roles
ny-02-ops:~$ echo role[blerk]
role[blerk]
ny-02-ops:~$ echo role[sugar]
role[sugar]
ny-02-ops:~$ mkdir roles
ny-02-ops:~$ echo role[blerk]
role[blerk]

这是我的"WTF"时刻

Here's my "WTF" moment

ny-02-ops:~$ echo role[sugar]
roles
ny-02-ops:~$ mkdir roleb
ny-02-ops:~$ echo role[blerk]
roleb

我知道那里有一个bash专家可以解释这一点,我非常感谢知道发生了什么事.

I know there's a bash guru out there who can explain this, and I would be very grateful to know what's going on.

推荐答案

全局括号中的方括号匹配其中的一个字符.如果具有元字符(*[...]等)的glob与文件名不匹配,则该glob不变地返回.

Square brackets in a glob match one of the characters inside. If a glob with metacharacters (*, [...], etc.) matches no filenames then the glob is returned unchanged.

echo "role[sugar]"
echo "role[blerk]"

这篇关于当目录存在与否时,bash shell给出奇怪的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆