使用 cd 导航到目录时运行 shell 脚本

Ole*_*iak 0 shell bash cd-command

当你cd进入它时,是否可以将脚本设置为在特定目录中运行?

例如,如果/foo是一个带有git项目的目录,是否可以cd /foo自动运行git status或某些npm脚本?

请不要将答案严格链接到git,想象这可能是任何其他命令/脚本。

Ste*_*itt 5

您可以替换cd为添加您想要的任何处理的函数:

cd() {
    builtin cd "$@"
    # Do whatever you want here
}
Run Code Online (Sandbox Code Playgroud)