编辑并重新加载 .bashrc 文件
Fumbani Banda
2023年1月30日
Bash
Bash Source
Bash Exec

本教程演示了如何编辑 .bashrc
文件并使用 source
命令或 exec
命令重新加载新更改。
什么是 .bashrc
.bashrc
是一个 bash shell 脚本,当 bash 以交互方式启动时,它就会运行。它初始化一个交互式 shell 会话。.bashrc
文件包含终端会话的配置。这些配置包括着色、shell 历史记录、完成、命令别名、环境变量等等。
.bashrc
是一个隐藏文件。要查看隐藏文件,请使用 -a
选项运行 ls
。-a
选项告诉 ls
列出所有条目,包括以 .
开头的条目,-l
选项告诉 ls
以长列表格式列出条目,以及 |
将 ls
输出通过管道传送到 head
命令,该命令打印输出的前十行。
$ ls -al | head
从下面的输出中,我们可以观察到我们有 .bashrc
文件。
total 94064
drwxr-xr-x 1 fumba fumba 4096 Nov 14 11:37 .
drwxr-xr-x 1 root root 4096 Sep 7 07:41 ..
-rw------- 1 fumba fumba 30965 Nov 13 23:16 .bash_history
-rw-r--r-- 1 fumba fumba 220 Sep 7 07:41 .bash_logout
-rw-r--r-- 1 fumba fumba 3771 Sep 7 07:41 .bashrc
drwxr-xr-x 1 fumba fumba 4096 Sep 7 21:35 .cache
drwx------ 1 fumba fumba 4096 Sep 7 15:05 .config
drwxr-xr-x 1 fumba fumba 4096 Sep 7 07:41 .landscape
drwxr-xr-x 1 fumba fumba 4096 Sep 23 06:41 .local
我们可以使用 cat
命令通过键入以下命令来显示 .bashrc
文件的内容。
$ cat .bashrc
编辑 .bashrc
并重新加载更改
使用你喜欢的文本编辑器在 .bashrc
文件的末尾添加以下函数。该函数在调用时显示该特定日期的日期。
date_today(){
date '+Today is %A, %B %d, %Y.'
}
保存更改后,我们可以通过运行以下命令重新加载 .bashrc
以反映新更改。source
命令读取并执行 .bashrc
文件的内容。
$ source .bashrc
重新加载 .bashrc
文件中更改的另一种方法是运行 exec bash
。exec bash
命令用一个新实例替换当前的 bash shell。
$ exec bash
要调用我们在 .bashrc
文件中创建的函数,请键入函数的名称,如下所示。
$ date_today
上面函数的输出打印当前日期。
Today is Sunday, November 14, 2021.
Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
作者: Fumbani Banda