1. 程式人生 > >Linux中profile、bashrc、bash_profile之間的區別和聯絡

Linux中profile、bashrc、bash_profile之間的區別和聯絡

/etc/profile:此檔案為系統的每個使用者設定環境資訊,當用戶第一次登入時,該檔案被執行.並從/etc/profile.d目錄的配置檔案中搜集shell的設定.

英文描述為:

# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

所以如果你有對/etc/profile有修改的話必須得重啟你的修改才會生效,此修改對每個使用者都生效。

/etc/bashrc:為每一個執行bash shell的使用者執行此檔案.當bash shell被開啟時,該檔案被讀取.

英文描述為:

# /etc/bashrc

# System wide functions and aliases
# Environment stuff goes in /etc/profile

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

如果你想對所有的使用bash的使用者修改某個配置並在以後開啟的bash都生效的話可以修改這個檔案,修改這個檔案不用重啟,重新開啟一個bash即可生效。

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

此檔案類似於/etc/profile,也是需要需要重啟才會生效,/etc/profile對所有使用者生效,~/.bash_profile只對當前使用者生效。

~/.bashrc:該檔案包含專用於你的bash shell的bash資訊,當登入時以及每次開啟新的shell時,該檔案被讀取.(每個使用者都有一個.bashrc檔案,在使用者目錄下)

此檔案類似於/etc/bashrc,不需要重啟生效,重新開啟一個bash即可生效,  /etc/bashrc對所有使用者新開啟的bash都生效,但~/.bashrc只對當前使用者新開啟的bash生效。

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

另外,/etc/profile中設定的變數(全域性)的可以作用於任何使用者,而~/.bashrc等中設定的變數(區域性)只能繼承/etc/profile中的變數,他們是"父子"關係.
 
~/.bash_profile 是互動式、login 方式進入bash 執行的;
~/.bashrc 是互動式 non-login 方式進入bash 執行的;
通常二者設定大致相同,所以通常前者會呼叫後者。