在shell函数中调用内置函数时阻止递归

时间:2017-05-13 20:37:27

标签: bash shell cd

我在bashrc中添加了以下函数: -

function cd(){
        echo "user switching to" $1
        cd $1
}

重新加载.bashrc后,尝试cd在临时目录(任何目录)中给出下面的递归错误: -

user switching to temp/
user switching to temp/
user switching to temp/
user switching to temp/
user switching to temp/
user switching to temp/
user switching to temp/
user switching to temp/
user switching to temp/
bash: echo: write error: Bad address
user switching to temp/
user switching to temp/
user switching to temp/
user switching to temp/
user switching to temp/
user switching to temp/
user switching to temp/
user switching to temp/

我希望方法名称为cd,因为我想在用户使用cd命令时执行某些逻辑。

1 个答案:

答案 0 :(得分:0)

您可以在bash中使用builtin命令:

function cd(){
        echo "user switching to $1"
        builtin cd "$@"
}