本文介绍了bash在更改目录(cd)之后执行shell函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
试图找到一种在更改目录后在BASH中执行功能的方法.
Trying to find a way to execute a function within BASH after changing into a directory.
例如,
# cd code/project/blah
"Your latest modified files are main.cc blah.hpp blah.cc"
(~/code/project/blah) # _
通过上述内容,我希望能够将其他功能包装在bash命令周围.
With the above I'm hoping to be able to wrap other functionality around the bash command.
希望通过zsh钩子函数找到一些东西 http://zsh.sourceforge.net/Doc/Release/Functions.html#SEC45
Was hoping to find something along the lines of zsh hook functionshttp://zsh.sourceforge.net/Doc/Release/Functions.html#SEC45
推荐答案
不要忘记 pushd
和 popd
,除非您从不使用它们.我会这样做:
Don't forget about pushd
and popd
, unless you never use them. I'd do this:
PS1='(\w) \$ '
chdir() {
local action="$1"; shift
case "$action" in
# popd needs special care not to pass empty string instead of no args
popd) [[ $# -eq 0 ]] && builtin popd || builtin popd "$*" ;;
cd|pushd) builtin $action "$*" ;;
*) return ;;
esac
# now do stuff in the new pwd
echo Your last 3 modified files:
ls -t | head -n 3
}
alias cd='chdir cd'
alias pushd='chdir pushd'
alias popd='chdir popd'
这篇关于bash在更改目录(cd)之后执行shell函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!