shell glob中的“或”

The*_*Cat 6 osx shell shell-script wildcards

我想 ls 文件名中包含子字符串“s1r”、“s2r”、“s3r”或“s19r”的文件。

我快到了!

拙劣的尝试:

ls *s[123][9?]r*
Run Code Online (Sandbox Code Playgroud)

上面只给了我包含子字符串的文件

s19r

尽管

ls *s[1-3]|[19]r*
Run Code Online (Sandbox Code Playgroud)

返回

-bash: [19]r*: 命令未找到

ls: *s[1-3]: 没有那个文件或目录

也就是说,它不识别或 | 运算符 - 这是有道理的,因为它也用于管道。

如何 ls 包含“s1r”、“s2r”、“s3r”或“s19r”的文件?

mic*_*has 6

使用ls *s1r* *s2r* *s3r* *s19r*.

如果您关心不存在的文件,您可以设置 nullglob 选项:

          nullglob
                  If  set,  bash allows patterns which match no files (see
                  Pathname Expansion above) to expand to  a  null  string,
                  rather than themselves.
Run Code Online (Sandbox Code Playgroud)

如果您的 shell 不是 bash,则可能有类似的方法。看看它的手册页。