如何告诉 bash 在 Python 脚本中专门转义通配符

Mar*_*ari 1 python bash shell wildcard

我有一个脚本。它需要参数,但我希望它在收到字符串“ *”时将其自动完成为列表变量中的匹配字符串。

不幸的是,如果用户不使用反斜杠转义通配符来转义星号,这是不可能的:

foo.py ba\*  # Will expand to "bar"
Run Code Online (Sandbox Code Playgroud)

不这样做会导致 bash 将其扩展为工作目录中的文件,然后将其传递给我的脚本。

告诉 bash 不自动扩展星号并将其传递给我的脚本需要什么。也许是安装脚本?

Fre*_*ihl 5

set -o noglob
Run Code Online (Sandbox Code Playgroud)

将阻止 bash 扩展*.

删除 set +o noglob