我可以让 bash 停止解析和验证某个别名吗?

iLi*_*irt 8 bash bashrc bash-alias

我正在使用 Fedora 25,并在我的 bash 配置文件中添加了以下别名:

alias releasenotes="dnf updateinfo --refresh info `dnf check-update | cut -d '.' -f 1 | xargs` | less"
Run Code Online (Sandbox Code Playgroud)

(我不能直接使用,dnf updateinfo info因为https://bugzilla.redhat.com/show_bug.cgi?id=1405191

我的别名有效,但该命令大约需要 10 秒才能运行,并且由于 bash 在获取配置文件时解析和验证所有别名,因此创建新 shell 会导致 10 秒挂起。这很烦人。

有什么办法可以让 bash 不尝试解析和验证别名——或者只是那个?

小智 9

我最好的猜测是你应该在别名定义周围使用单引号

我知道当使用双引号时,shell 变量会在别名定义阶段(如您所说的解析和验证)和反引号或 shell 替换(如 $(command))替换为其内容。

更好的解释是在这个Unix SE 问题中

如果这无助于再次加快提示加载速度,请定义一个 shell 函数而不是别名。

编辑:不要忘记将 cut 参数交换为提到的 quixotic 等双引号。


qui*_*tic 9

bash正在解释您引用的字符串,并且该解释执行嵌入的dnf check-update命令。这个执行是在别名定义过程中占用时间的,而不是dnf updateinfo你正在别名化的主要命令。尝试一个人为的例子,sleep并注意alias它本身是如何花费 5 秒的:

alias sleep5="echo 'wake' ; `sleep 5` ; echo 'done'"
Run Code Online (Sandbox Code Playgroud)

使用单引号避免解释:

alias releasenotes='dnf updateinfo --refresh info `dnf check-update | cut -d "." -f 1 | xargs` | less'
Run Code Online (Sandbox Code Playgroud)

不要忘记将cut参数交换为双引号。