這幾個設定檔名字相似、位置分散,執行時機卻完全不同。最準的辦法就是實測。
在 /etc 與 /home/username 底下的 bashrc、profile 中,都加上這幾行:
echo `date` >> /home/zhennn/priority echo "/home/zhennn/.bashrc" >> /home/zhennn/priority echo >> /home/zhennn/priority
然後做兩個動作:一、開啟一個終端;二、用 su - zhennn 切換使用者登入。觀察 priority 檔的記錄順序。
# 開啟終端時 /etc/bash.bashrc /home/zhennn/.bashrc # su - zhennn 登入時 /etc/bash.bashrc /etc/profile /home/zhennn/.bashrc /home/zhennn/.profile
一個使用者進入終端時,bashrc 會被執行一次,而且 /etc 先於家目錄。
使用者「登入」時,profile 也會被執行;同一個目錄中,bashrc 先於 profile。
因此,要設定「所有使用者都生效」的環境變數,放在 /etc/profile 或 /etc/bash.bashrc;只對單一使用者生效的,放家目錄。而 .profile 通常會再去 source 一次 .bashrc,這就是為什麼登入 shell 也會載入 bashrc 的設定。