在 shell 函数中调用内置函数时防止递归
Preventing recursion when calling a builtin inside a shell function
我在 bashrc 中添加了以下功能:-
function cd(){
echo "user switching to"
cd
}
重新加载 .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 命令时执行一些逻辑。
您可以在bash中使用builtin
命令:
function cd(){
echo "user switching to "
builtin cd "$@"
}
我在 bashrc 中添加了以下功能:-
function cd(){
echo "user switching to"
cd
}
重新加载 .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 命令时执行一些逻辑。
您可以在bash中使用builtin
命令:
function cd(){
echo "user switching to "
builtin cd "$@"
}