shell登錄時的文件執行順序

想必大家都在linux系統中設置過環境變量,有沒有感覺不知道應該在哪個文件裏面設置呢?比如~/.profile, ~/.bash_profile, ~/.bashrc , /etc/profile , 等等。

其實這些文件會在不同的場景下被運行。

shell分爲login shell , non-login shell , interactive shell , non-interactive shell 。

比如當你ssh example.com之後,獲取的就是interactive login shell , 

當你ssh example.com "uptime" , 獲取的就是non-interactive login shell ,

當你在登錄的機器上su user之後,獲取的就是interactive non-login shell,

當你在登錄的機器上su - user之後,獲取的就是interactive login shell 。

下面是在不同場景下執行文件的先後順序,A, B, C表示先執行A,再執行B,再執行C。

B1,B2,B3表示按順序找文件,先找到誰就只執行誰。

+----------------+-----------+-----------+------+
|                |Interactive|Interactive|Script|
|                |login      |non-login  |      |
+----------------+-----------+-----------+------+
|/etc/profile    |   A       |           |      |
+----------------+-----------+-----------+------+
|/etc/bash.bashrc|           |    A      |      |
+----------------+-----------+-----------+------+
|~/.bashrc       |           |    B      |      |
+----------------+-----------+-----------+------+
|~/.bash_profile |   B1      |           |      |
+----------------+-----------+-----------+------+
|~/.bash_login   |   B2      |           |      |
+----------------+-----------+-----------+------+
|~/.profile      |   B3      |           |      |
+----------------+-----------+-----------+------+
|BASH_ENV        |           |           |  A   |
+----------------+-----------+-----------+------+
|                |           |           |      |
+----------------+-----------+-----------+------+
|                |           |           |      |
+----------------+-----------+-----------+------+
|~/.bash_logout  |    C      |           |      |
+----------------+-----------+-----------+------+


鏈接

https://shreevatsa.wordpress.com/2008/03/30/zshbash-startup-files-loading-order-bashrc-zshrc-etc/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章