用别名覆盖内置命令 [英] Override a builtin command with an alias
问题描述
我正在尝试创建一个覆盖cd
命令的别名.这将在真实" cd
之前和之后执行脚本.
I am trying to make an alias that overrides the cd
command. This is going to execute a script before and after the "real" cd
.
这是我到目前为止所拥有的:
Here is what I have so far:
alias cd="echo before; cd $1; echo after"
这会执行echo before
和echo after command
,但它会始终更改目录~
This executes the echo before
and echo after command
however it always changes directory ~
我该如何解决?
我也尝试过cd(){ echo before; cd $1; echo after; }
,但是它反复地重复"before".
I also tried cd(){ echo before; cd $1; echo after; }
however it repetedly echos "before".
推荐答案
我也尝试过
cd(){ echo before; cd $1; echo after; }
,但是它反复地重复"before".
I also tried
cd(){ echo before; cd $1; echo after; }
however it repetedly echos "before".
因为它递归调用您定义的cd
.要解决此问题,请使用builtin
关键字,例如:
because it calls recursively the cd
defined by you. To fix, use the builtin
keyword like:
cd(){ pwd; builtin cd "$@"; pwd; }
Ps:无论如何,恕我直言,重新定义shell内置程序不是最好的主意.
这篇关于用别名覆盖内置命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!