bash - 是否可以在进入目录时自动进行一些 shell 调用?

标签 bash shell sh

就像一个本地的.bashrc文件,每次进入目录都是sourced。 如何实现?

最佳答案

您可以使用别名:

$ echo 'echo "execute something for $PWD"' > tests/.cdrc
$ _cd()
{
    \cd $1
    [ -r .cdrc ] && . .cdrc
}

此函数首先更改为参数指定的目录,检查文件 .cdrc 是否可读并获取它。

$ alias cd=_cd

然后

$ cd tests
execute something for /path/to/tests

关于bash - 是否可以在进入目录时自动进行一些 shell 调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17119725/

相关文章:

linux - 需要帮助 shell 脚本复制多个目录并打包到 tar 中

linux - 我想读取第一行值并从第二行值中减去它并继续直到结束

linux - Bourne Shell (bin/sh) 中的嵌套变量

bash - 使用符号链接(symbolic link)时 sh 和 bash 之间的区别

bash - 在 bash 脚本中使用 ping 检查主机可用性

mongodb - bash中嵌套引号的问题:通过ssh在docker容器中查询的mongodump

bash - Github 操作 : Why an intermediate command failure in shell script would cause the whole step to fail?

linux - 带文件路径参数的 Windows 10 Bash 和 Google Chrome

linux - 从终端执行命令与从脚本执行命令 - 带通配符的路径 - 防止 glob 扩展

java - 如何将 java 方法的输出分配给 bash 脚本变量