1. 程式人生 > >Linux基礎知識之bashrc和profile的用途和區別

Linux基礎知識之bashrc和profile的用途和區別

使用終端ssh登入Linux作業系統的控制檯後,會出現一個提示符號(例如:#或~),在這個提示符號之後可以輸入命令,Linux根據輸入的命令會做迴應,這一連串的動作是由一個所謂的Shell來做處理。

Shell是一個程式,最常用的就是Bash,這也是登入系統預設會使用的Shell。

bashrc和profile都是Shell的啟動設定檔案(其實這兩個檔案也是Shell指令碼),可以為當前的Shell初始化環境變數等,它們類似Microsoft DOS/Windows下的autoexec.bat檔案。

下面詳細介紹bashrc和profile的用途和區別:

1. 針對個別使用者

使用者HOME(家)目錄/.bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.

使用者HOME(家)目錄/.profile
# ~/.profile: executed by Bourne-compatible login shells.

2. 針對全體使用者

/etc/bash.bashrc
# System-wide .bashrc file for interactive bash(1) shells.

/etc/profile
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))

從上面的英文描述可以知道,bashrc和profile的差異在於:

1. bashrc是在系統啟動後就會自動執行。

2. profile是在使用者登入後才會執行。

3. 進行設定後,可運用source bashrc命令更新bashrc,也可運用source profile命令更新profile。
PS:通常我們修改bashrc,有些linux的發行版本不一定有profile這個檔案

4. /etc/profile中設定的變數(全域性)的可以作用於任何使用者,而~/.bashrc等中設定的變數(區域性)只能繼承/etc/profile中的變數,他們是”父子”關係。

===============================================================================

另外,需要補充說明介紹bashrc相關的幾個檔案:

~/.bash_profile: 每個使用者都可使用該檔案輸入專用於自己使用的shell資訊,當用戶登入時,該檔案僅僅執行一次!預設情況下,他設定一些環境變數,執行使用者的.bashrc檔案。

~/.bash_logout: 當每次退出系統(退出bash shell)時,執行該檔案。

~/.bash_profile 是互動式、login方式進入bash執行的,~/.bashrc是互動式non-login方式進入bash執行的,通常二者設定大致相同,所以通常前者會呼叫後者。