/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
————————————————
版權聲明:本文為CSDN博主「siberiawolf61」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/u010048823/article/details/51871825


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM