cd(){ echo before; cd $1; echo after; }然而我也尝试过它"在之前"重复回声.
因为它以递归方式调用cd您定义的.要修复,请使用以下builtin关键字:
cd(){ pwd; builtin cd "$@"; pwd; }
Run Code Online (Sandbox Code Playgroud)
Ps:无论如何,恕我直言不是重新定义shell内置的最好的主意.
只是添加到@ jm666 的答案:
要使用函数覆盖非内置函数,请使用command. 例如:
ls() { command ls -l; }
Run Code Online (Sandbox Code Playgroud)
这相当于alias ls='ls -l'.
command也适用于内置函数。所以,你cd也可以写成:
cd() { echo before; command cd "$1"; echo after; }
Run Code Online (Sandbox Code Playgroud)
要绕过函数或别名并运行原始命令或内置命令,您可以将 a\放在开头:
\ls # bypasses the function and executes /bin/ls directly
Run Code Online (Sandbox Code Playgroud)
或使用command本身:
command ls
Run Code Online (Sandbox Code Playgroud)