有没有办法为某些 bash 命令禁用星号 * 插值?

Ber*_*ala 7 command-line bash wildcards

我希望能够写入* 我的脚本,并保持*原样(即,没有扩展)。

Bash 会尝试用匹配此模式的本地文件替换它,如果此模式不存在,bash 将不加修改地传递星号。

我不想逃。我知道这是可能的( *, '*' )。

例如

myscript --arg *   --- will pass local files to the script
myscript --arg=*   --- will pass "--arg=*", since there are no filenames starting with "--arg=<...>"
Run Code Online (Sandbox Code Playgroud)

我可以告诉 bash 在某些情况下跳过通配符解释吗?例如,以myscript?开头的命令

小智 16

是的。您可以使用set -f

set -f
echo *  # prints *
# turn glob expansion back on, if you want:
set +f
Run Code Online (Sandbox Code Playgroud)

如果您选择此路线,则必须在调用脚本的终端中设置它,而不是在脚本本身中,因为它*会在作为参数到达您的脚本之前扩展。

正如在评论中提到的,它可能会更好,只是逃避*,要么通过'*'"*"\*