是否有一个击勾找出CWD更改时? [英] Is there a hook in Bash to find out when the cwd changes?

查看:202
本文介绍了是否有一个击勾找出CWD更改时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常使用的zsh,它提供了chpwd()钩。那就是:如果CWD是由CD内置改变,zsh的自动调用方法chpwd(),如果它的存在。这允许设置的变量和别名这取决于CWD。

现在我想将我的.zshrc来砸这一点,但发现chpwd()不被认可的bash。是一个类似的功能已经在bash存在?我知道,重新定义了CD的工作(见下文),但我的目标更优雅的解决方案。

 功能的cd()
{
    内置CD $ @
    chpwd
}


解决方案

您将不得不使用一个DEBUG 陷阱 PROMPT_COMMAND

例如:

 陷阱chpwd DEBUG#每个命令之前调用该函数PROMPT_COMMAND = chpwd#每个命令后调用函数

请注意,在 PROMPT_COMMAND 定义的函数之前运行的每个提示符下,不过,即使是空的。

I am usually using zsh, which provides the chpwd() hook. That is: If the cwd is changed by the cd builtin, zsh automatically calls the method chpwd() if it exists. This allows to set up variables and aliases which depend on the cwd.

Now I want to port this bit of my .zshrc to bash, but found that chpwd() is not recognized by bash. Is a similar functionality already existing in bash? I'm aware that redefining cd works (see below), yet I'm aiming for a more elegant solution.

function cd()
{
    builtin cd $@
    chpwd
}

解决方案

You would have to use a DEBUG trap or PROMPT_COMMAND.

Examples:

trap chpwd DEBUG        # calls the function before each command

PROMPT_COMMAND=chpwd    # calls the function after each command

Note that the function defined in PROMPT_COMMAND is run before each prompt, though, even empty ones.

这篇关于是否有一个击勾找出CWD更改时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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