/etc/profile、~/.bash_profile等幾個文件的執行過程
在登錄Linux時要執行文件的過程如下:
在 剛登錄Linux時,首先啓動 /etc/profile 文件,然後再啓動用戶目錄下的 ~/.bash_profile、 ~/.bash_login或 ~/.profile文件中的其中一個,執行的順序爲:~/.bash_profile、 ~/.bash_login、 ~/.profile。如果 ~/.bash_profile文件存在的話,一般還會執行 ~/.bashrc文件。因爲在 ~/.bash_profile文件中一般會有下面的代碼:
if [ -f ~/.bashrc ] ; then
. ./bashrc
fi
~/.bashrc中,一般還會有以下代碼:
if [ -f /etc/bashrc ] ; then
. /etc/bashrc
fi
所以,~/.bashrc會調用 /etc/bashrc文件。最後,在退出shell時,還會執行 ~/.bash_logout文件。
執 行順序爲:/etc/profile -> (~/.bash_profile | ~/.bash_login | ~/.profile) -> ~/.bashrc -> /etc/bashrc -> ~/.bash_logout
關於各個文件的作用域,在網上找到了以下說明:
(1)/etc/profile: 此文件爲系統的每個用戶設置環境信息,當用戶第一次登錄時,該文件被執行. 並從/etc/profile.d目錄的配置文件中搜集shell的設置。
(2)/etc/bashrc: 爲每一個運行bash shell的用戶執行此文件.當bash shell被打開時,該文件被讀取。
(3)~/.bash_profile: 每個用戶都可使用該文件輸入專用於自己使用的shell信息,當用戶登錄時,該文件僅僅執行一次!默認情況下,他設置一些環境變量,執行用戶的.bashrc文件。
(4)~/.bashrc: 該文件包含專用於你的bash shell的bash信息,當登錄時以及每次打開新的shell時,該該文件被讀取。
(5)~/.bash_logout: 當每次退出系統(退出bash shell)時,執行該文件. 另外,/etc/profile中設定的變量(全局)的可以作用於任何用戶,而~/.bashrc等中設定的變量(局部)只能繼承/etc /profile中的變量,他們是"父子"關係。
(6)~/.bash_profile 是交互式、login 方式進入 bash 運行的~/.bashrc 是交互式 non-login 方式進入 bash 運行的通常二者設置大致相同,所以通常前者會調用後者。
我做了個實驗,在/etc/profile,/etc/bashrc,~/.bashrc和~/.bash_profile文件的最後追加同一個變量分別賦予不同的值,實驗結果表明變量最後的值爲~/.bash_profile裏的值。(4個文件都沒有修改其他設置,都是安裝系統後的默認值。)
再有就是4個文件都追加一個值到同一個文件,開機後查看該文件內容的順序爲:
/etc/profile
/etc/bashrc
~/.bashrc
~/.bash_profile
----------------------
其他文章:
redhat bash 初始化設置
先說明三個概念
登錄shell
正常登錄程序啓動的shell.既登錄成功後緊接着給登錄用戶啓動的shell.
非登錄交互式shell
這個shell的工作方式是交互式的,等用戶輸入,然後執行,再等用戶輸入。顯然登錄shell就是一個交互式shell。
如下,我們可獲得一個交互式非登錄shell:
[root@localhost ~]# bash
[root@localhost ~]# pwd
/root
非交互式shell
爲運行一個shell腳本啓動的shell.
以FC5的bash爲例,跟shell環境配置相關的文件以下幾個,
/etc/profile
/etc/profile.d/*.sh
/etc/bashrc
~/.bash_profile
~/.bashrc
有時你會發現定義一個別名,有時好像在任意一個文件裏定義都可以起作用,有時好像又不起作用,那是爲什麼呢?這些配置文件各自互責了什麼工作?相互的關係是怎麼樣的?跟前面介紹的不同種類的shell的關係是如何的呢?下面對每個文件單獨進行說明。
/etc/profile
Linux規定,當啓動一個登錄shell會執行這個腳本. 測試過程如下:
把LIST的定義加到/etc/profile文件的未尾並保存. 如下:
alias LIST='ls -l'
把所有其它shell配置文件或目錄改名,這樣系統就找不到那些shell腳本了,不會執行,重而避免其它配置文件的干擾。如下:
[root@localhost ~]# mkdir /etc/profile.bak
[root@localhost ~]# mv /etc/profile.d/* -t /etc/profile.bak/
[root@localhost ~]# mv /etc/bashrc /etc/bashrc.bak
[root@localhost ~]# mv ~/.bash_profile ~/.bash_profile.bak
[root@localhost ~]# mv ~/.bashrc ~/.bashrc.bak
交互式shell,並測試過程如下:
[root@localhost ~]# bash
bash-3.1# LIST
bash: LIST: command not found
bash-3.1# exit
exit
[root@localhost ~]#
顯然啓動一個普通交互式shell的時候, shell配置文件/etc/profile不起作用
非交互式shell, 測試過程如下:
爲了驗證先寫一個測試腳本,如下:
#!/bin/bash
LIST
把這個腳本保存爲t.sh並加下可執行權限:
[root@localhost ~]# chmod a+x t.sh
[root@localhost ~]# ./t.sh
./t.sh: line 2: LIST: command not found
[root@localhost ~]#
顯然啓動一個非交互式shell時,shell配置文件/etc/profile不起作用
登錄shell,並測試過程如下:
Last login: Wed Nov 19 10:22:23 2008 from 192.168.0.97
-bash-3.1# LIST
total 160
drwxr-xr-x 2 root root 4096 Aug 14 12:24 Desktop
-rw-r--r-- 1 root root 3211 Nov 6 10:15 Session.vim
drwxr-xr-x 2 root root 4096 Nov 10 10:58 a
-rw-r--r-- 1 root root 126 Nov 12 12:42 a.txt
-rw-r--r-- 1 root root 261 Nov 6 15:23 a.zip
-rw-r--r-- 1 root root 157 Nov 6 15:23 aa.zip
-rw------- 1 root root 1054 Aug 14 11:59 anaconda-ks.cfg
-rw-r--r-- 1 root root 691 Nov 18 10:09 b.txt
-rw-r--r-- 1 root root 31671 Aug 14 11:58 install.log
-rw-r--r-- 1 root root 4155 Aug 14 11:50 install.log.syslog
-rw------- 1 root root 20310 Nov 17 13:51 mbox
drwxr-xr-x 2 root root 4096 Nov 17 17:22 shell
-rwxrwxrwx 1 root root 65 Nov 19 10:11 t.sh
drwxr-xr-x 14 root root 4096 Nov 5 15:34 test
-bash-3.1#
顯然啓動一個登錄shell時,shell配置文件/etc/profile會起作用
~/.bash_profile
這個文件跟/etc/profile起作用的時機是一樣的,都是隻在啓動一個登錄shell的時候纔會被source,跟/etc/profile不同的是,這裏的配置隻影響單個用戶,不對其它用戶產生影響。
/etc/bashrc與~/.bashrc
從字面上我們可以理解這兩個文件應該跟根bash相關,即 只要你啓動了一個bash類型的shell這兩文件的配置就將發生作用。如果你的shell是sh、csh或是ksh這兩個文件將不起作用。按前面的介 紹,可能很會猜測/etc/bashrc與~/.bashrc的關係跟/etc/profile與~/.bash_profile的關係一樣,一個是全局 的,一個是針對單個用戶的。從結果上看確實是這樣的,但實現過程卻是不一樣的。啓動一個bash時直接source ~/.bashrc, 而這~/.bashrc裏面會source
/etc/bashrc。
/etc/profile.d/*.sh
在fc5下這裏的腳本會在/etc/profile裏或是~/.bashrc裏同時source, 所以這裏的設置都是一些不同分類的全局環境設置。
總結在FC5下一個登錄bash的環境初始全過程是:
/etc/profile
|
--/etc/profile.d/*
~/.bash_profile
|
--~/.bashrc
|
--/etc/bashrc
|
--/etc/profile.d/*
一個普通交互式bash的初始全過程是:
~/.bashrc
|
--/etc/bashrc
|
--/etc/profile.d/*
對於非交互式bash的初始全過程是:
不重新source 任何新的shell腳本,只繼承當前shell的設置.