在 CentOS 環境下,環境變量的優先級從高到低如下:
export
命令設置的環境變量。.bashrc
或 .bash_profile
文件中設置的環境變量。.bash_profile
通常在登錄 shell 時執行,而 .bashrc
在交互式非登錄 shell 時執行。如果 .bash_profile
中有 source ~/.bashrc
語句,那么 .bashrc
中的設置也會生效。/etc/profile
和 /etc/bashrc
中設置的環境變量。/etc/profile
對所有用戶生效,而 /etc/bashrc
只對運行 bash 的用戶生效。通常情況下,/etc/profile
會 source /etc/bashrc
,以便讓全局配置對所有用戶生效。.bash_aliases
文件中設置的環境變量,如果 .bashrc
中有 source ~/.bash_aliases
語句,那么 .bash_aliases
中的設置也會生效。/etc/environment
文件設置的環境變量。這個文件在系統啟動過程中被讀取,對所有用戶生效。需要注意的是,不同 shell(如 zsh、fish 等)可能有不同的環境變量配置文件和優先級順序。上述答案適用于 CentOS 系統下的 bash shell。