我的bash
(4.1)目录堆栈通常有十几个或更多条目。我想用dirs
with替换输出dirs -v
,所以我再也不用玩“猜魔数”pushd
了。
我的部分解决方案
我替换为使用dirs
执行的函数:dirs -v
command
dirs()
{
# "command" builtin prevents infinite recusion by suppressing lookup
# of function and alias names.
command dirs -v "${@}"
}
(更新:根据气动的建议,我现在使用builtin
代替command
。它不能解决这个问题,但它稍微安全一些。)
dirs
输出现在可读,但pushd
仍然popd
产生旧的 vomit-of-slashes:
$ pushd ~/just/one/more/and/ill/quit/i/promise
~/just/one/more/and/ill/quit/i/promise ~/tmp/bash/bash-4.1...
may/give/rise/to/dom /oh/no/not/again /var/adm/log /omg/wt...
va/lang ~/doc/comp/java/api/java/util/regex barf barf barf...
dirs
用别名替换我的函数后,我得到了相同的(缺少)结果:
alias dirs='command dirs -v "${@}"'
解决方法
pushd
我最终通过覆盖和得到了我想要的输出popd
:
pushd()
{
command pushd "${@}" >/dev/null &&
dirs
}
# popd is similar.
这可行,但它需要覆盖三个内置函数而不是一个。此外,这可能是一个不完美的模仿,pushd
并且popd
在某些极端情况下我没有想到。我宁愿dirs
只覆盖。
我的问题
为什么覆盖dirs
不起作用? 根据bash
的man
页面,在pushd
和下popd
:
如果
pushd
命令成功,dirs
也会执行 a。
那么为什么pushd
并且popd
似乎调用了内置dirs
函数而不是函数或别名呢?
关于bash
文档的脚注
在线手册dirs
中缺少“a is perform as well”的段落,但它们出现在页面和中。我的4.1 文档和当前的 4.4 文档都以相同的方式不一致,这表明 4.4(如果我有的话)的行为相同。bashref.*
man
bash.*
builtins.*
bash