bash - 自定义 cd 命令

标签 bash shell ubuntu cd


通常,我在 .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/

相关文章:

bash - 在 shell 脚本中减去字符串(即数字)

regex - 使用 Sed 将字符串变量替换为字符串变量

java - 使用Java的getRuntime.exec()运行Linux Shell命令: How?

ubuntu - 用于重置 Evolution 密码的 shell 脚本

ruby-on-rails - 服务器重启后如何重启 RoR 服务

c - 是否可以将多个输入通过管道传输到一个程序中?

mysql - 在范围内选择一个数字,它不会与 bash shell 中存在的数字冲突

linux - 将 AWK 结果分配给变量

linux - Linux下执行FTP命令文件

c - gnu c引用手册在哪个debian/ubuntu软件包中?