編輯並重新載入 .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