无限递归别名`cd` [英] Infinite recursion aliasing `cd`

查看:34
本文介绍了无限递归别名`cd`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的任何一个终端上记录我最近的 cd.我认为一个很好的方法是编写一个简单的 bash 脚本来包装 cd:

I want to record my most recent cd across any one of my terminals. I thought a good way to do this would be to write a simple bash script wrapping cd:

#!/bin/bash
cd $1 && echo `pwd` > /tmp/.cwd

由于我希望 cd 出现在我的终端进程中,所以我需要使用 运行脚本.Bettercd.sh,对吗?

Since I want the cd to occur in my terminal's process, I need to run the script with . bettercd.sh, correct?

我的问题来了:如果我将 cd 别名为这个新的 .Bettercd.sh,我的shell 还用 扩展了脚本内部的cd.Bettercd.sh -- 无限递归.

Here comes my issue: If I alias cd to this new . bettercd.sh, my shell also expands the cd inside of the script with the . bettercd.sh -- infinite recursion.

有没有办法从不同的名称但具有相同的行为来调用 cd ?换句话说,是否有一些命令的行为与 cd 完全(或非常相似)我可以在我的 shell 脚本中使用而不会在我使用别名 cd每天?

Is there any way to call cd from a different name but with the same behavior? To put it another way, is there some command that behaves exactly (or very similar to) cd that I can use in my shell script without noticing a difference when I use the aliased cd day to day?

我选择的 shell 是 zsh,如果这在某种程度上是相关的.

My shell of choice is zsh, if that's relevant in some way.

感谢您的帮助.

推荐答案

由于您使用的是 zsh,因此您可以使用 builtin cd 代替 cd.这会强制 shell 使用内置命令,而不是递归调用您的别名.

Since you are using zsh, you can use builtin cd in place of cd. This forces the shell to use the builtin command instead of recursively calling your alias.

builtin 在标准的 bourne shell 中不存在.如果您需要在其他 shell 中使用它,请尝试在 cd 前面加上一个反斜杠,如下所示:\cd.它可以绕过别名,但不能绕过 shell 函数.

builtin does not exist in standard bourne shell. If you need this to work in other shells, try prefixing cd with a backslash like this: \cd. It works to bypass aliases but not shell functions.

这篇关于无限递归别名`cd`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆