Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

bashrc 與 profile 到底什麼時候被執行

這幾個設定檔名字相似、位置分散,執行時機卻完全不同。最準的辦法就是實測。

實驗方法

在 /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 的設定。

Copyright © Linux教程網 All Rights Reserved