bash在更改目录(cd)之后执行shell函数

编程入门 行业动态 更新时间:2024-10-28 14:26:52
本文介绍了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钩子函数找到一些东西 zsh.sourceforge/Doc/Release/Functions.html#SEC45

Was hoping to find something along the lines of zsh hook functions zsh.sourceforge/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函数

本文发布于:2023-07-16 03:58:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117619.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   目录   bash   shell   cd

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!