通常,我在 .bashrc
中保留目录特定设置,每当我更改目录时,执行命令 source .bashrc
以使这些设置生效。
现在我正在考虑在 ~/.bashrc
中操作 cd 命令,所以每当我 cd 到新目录并且如果那里存在任何 .bashrc 时,它将自动加载。
类似于此 cd $1; source .bashrc
(我已经验证 $1 是有效路径),但问题是 cd 是 shell 构建,所以它是一个递归循环( cd 总是指向修改后的 cd )。我们没有 cd 的 elf 文件(通常我们有其他命令,即 scp 或其他命令)。那么我该如何实现呢?
此外,如果支持 shopt -s cdspell
,那么我还需要在 $1 的参数中使用 cd 拼写路径。
最佳答案
你想要“内置”命令;
builtin shell-builtin [arguments]
Execute the specified shell builtin, passing it arguments, and return its exit status. This is useful when defining a function whose name is the same as a shell builtin, retaining the functionality of the builtin within the function. The cd builtin is commonly redefined this way. The return status is false if shell-builtin is not a shell builtin command.
发件人:http://linux.die.net/man/1/bash
所以,你可以有类似的东西(未经测试,手边也没有 bash);
function cd() {
builtin cd $1 \
&& test -e .bashrc \
&& source .bashrc
}
关于bash - 自定义 cd 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10026803/