bash 进入目录时执行bash函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3360738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute a bash function upon entering a directory
提问by Paul Biggar
I'd like to execute a particular bash function when I enter a new directory. Somethink like:
当我进入一个新目录时,我想执行一个特定的 bash 函数。有人认为:
alias cd="cd $@ && myfunction"
$@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 pushdand popd.
$@在那里不起作用,添加反斜杠也无济于事。我也有点担心弄乱 cd,如果这适用于更改目录的其他命令,比如pushd和,那就太好了popd。
Any better aliases/commands?
任何更好的别名/命令?
采纳答案by falstro
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.
那应该这样做。它适用于所有命令,并在显示提示之前被触发。
回答by Paused until further notice.
Aliases don't accept parameters. You should use a function. There's no need to execute it automatically every time a prompt is issued.
别名不接受参数。你应该使用一个函数。没有必要在每次发出提示时自动执行它。
function cd () { builtin cd "$@" && myfunction; }
The builtinkeyword allows you to redefine a Bash builtin without creating a recursion. Quoting the parameter makes it work in case there are spaces in directory names.
该builtin关键字允许您在不创建递归的情况下重新定义 Bash 内置函数。引用参数使其在目录名称中有空格的情况下起作用。
The Bash docssay:
该猛砸文档说:
For almost every purpose, shell functions are preferred over aliases.
对于几乎所有用途,shell 函数都比别名更受欢迎。
回答by cxreg
回答by jkramer
I've written a ZSH script utilizing the callback function chpwdto source project specific ZSH configurations. I'm not sure if it works with Bash, but I think it'll be worth a try. If it doesn't find a script file in the directory you're cd'ing into, it'll check the parent directories until it finds a script to source (or until it reaches /). It also calls a function unmagicwhen cd'ing out of the directory, which allows you to clean up your environment when leaving a project.
我编写了一个 ZSH 脚本,使用回调函数chpwd来获取项目特定的 ZSH 配置。我不确定它是否适用于 Bash,但我认为值得一试。如果它在您正在 cd 进入的目录中找不到脚本文件,它将检查父目录,直到找到要获取的脚本(或直到到达/)。它还unmagic在 cd 退出目录时调用一个函数,它允许您在离开项目时清理您的环境。
http://github.com/jkramer/home/blob/master/.zsh/func/magic
http://github.com/jkramer/home/blob/master/.zsh/func/magic
Example for a "magic" script:
“魔术”脚本的示例:
export BASE=$PWD # needed for another script of mine that allows you to cd into the projects base directory by pressing ^b
ctags -R --languages=Perl $PWD # update ctags file when entering the project directory
export PERL5LIB="$BASE/lib"
# function that starts the catalyst server
function srv {
perl $BASE/script/${PROJECT_NAME}_server.pl
}
# clean up
function unmagic {
unfunction src
unset PERL5LIB
}

