歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> /etc/profile和~/.bash_profile等文件的區別和聯系

/etc/profile和~/.bash_profile等文件的區別和聯系

日期:2017/3/3 11:32:01   编辑:Linux技術

本文系統環境:Centos7

/etc/profile和~/.bash_profile等文件的區別和聯系

對比說明

/etc/profile:為系統的每個用戶設置環境信息和啟動程序,當用戶第一次登錄時,該文件被執行,其配置對所有登錄的用戶都有效。當被修改時,必須重啟才會生效。英文描述:”System wide environment and startup programs, for login setup.” /etc/environment:系統的環境變量,/etc/profile是所有用戶的環境變量,前者與登錄用戶無關,後者與登錄用戶有關,當同一變量在兩個文件裡有沖突時,以用戶環境為准。

/etc/bashrc:為每個運行 bash shell 的用戶執行該文件,當 bash shell 打開時,該文件被執行,其配置對所有使用bash的用戶打開的每個bash都有效。當被修改後,不用重啟只需要打開一個新的 bash 即可生效。英文描述:”System wide functions and aliases.” ~/.bash_profile:為當前用戶設置專屬的環境信息和啟動程序,當用戶登錄時該文件執行一次。默認情況下,它用於設置環境變量,並執行當前用戶的 .bashrc 文件。理念類似於 /etc/profile,只不過只對當前用戶有效,也需要重啟才能生效。(注意:Centos7系統命名為.bash_profile,其他系統可能是.bash_login或.profile。)

~/.bashrc:為當前用戶設置專屬的 bash 信息,當每次打開新的shell時,該文件被執行。理念類似於/etc/bashrc,只不過只對當前用戶有效,不需要重啟只需要打開新的shell即可生效。

~/.bash_logout:為當前用戶,每次退出bash shell時執行該文件,可以把一些清理工作的命令放進這個文件。

/etc/profile.d/:此文件夾裡是除/etc/profile之外其他的”application-specific startup files”。英文描述為”The /etc/profile file sets the environment variables at startup of the Bash shell. The /etc/profile.d directory contains other scripts that contain application-specific startup files, which are also executed at startup time by the shell.” 同時,這些文件”are loaded via /etc/profile which makes them a part of the bash “profile” in the same way anyway.” 因此可以簡單的理解為是/etc/profile的一部分,只不過按類別或功能拆分成若干個文件進行配置了(方便維護和理解)。

注意事項

以上需要重啟才能生效的文件,其實可以通過
source xxx
暫時生效。文件的執行順序為:當登錄Linux時,首先啟動/etc/environment和/etc/profile,然後啟動當前用戶目錄下的~/.bash_profile,執行此文件時一般會調用~/.bashrc文件,而執行~/.bashrc時一般會調用/etc/bashrc,最後退出shell時,執行~/.bash_logout。簡單來說順序為:

(登錄時)/etc/environment –> /etc/profile(以及/etc/profile.d/裡的文件) –> ~/.bash_profile –> (打開shell時)~/.bashrc –> /etc/bashrc –> (退出shell時)~/.bash_logout

Copyright © Linux教程網 All Rights Reserved