在进入目录执行bash函数 [英] Execute a bash function upon entering a directory

查看:157
本文介绍了在进入目录执行bash函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,当我进入一个新的目录来执行特定bash函数。 Somethink这样的:

I'd like to execute a particular bash function when I enter a new directory. Somethink like:

alias cd="cd $@ && myfunction"

$ @ 这里是行不通的,并增加一个反斜杠于事无补。我也有点担心带CD搞乱,这将是很好,如果这个工作了,改变了目录,如 PUSHD 其他命令和 POPD

$@ doesn't work there, and adding a backslash doesn't help. I'm also a little worried about messing with cd, and it would be nice if this worked for other commands which changed directory, like pushd and popd.

任何更好的别名/命令?

Any better aliases/commands?

推荐答案

最简单的解决方案,我能想出是这样

The easiest solution I can come up with is this

myfunction() {
  if [ "$PWD" != "$MYOLDPWD" ]; then
    MYOLDPWD="$PWD";
    # strut yer stuff here..
  fi
}

export PROMPT_COMMAND=myfunction

这应该做到这一点。它会与所有命令的工作,并显示之前提示就会触发。

That ought to do it. It'll work with all commands, and will get triggered before the prompt is displayed.

这篇关于在进入目录执行bash函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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