一、介紹
/etc/profile
此文件為系統(tǒng)的每個用戶設(shè)置環(huán)境信息,當(dāng)用戶第一次登錄時,該文件被執(zhí)行。并從 /etc/profile.d 目錄的配置文件中收集 shell 的設(shè)置。如果你有對 /etc/profile 有修改的話必須得 source 一下你的修改才會生效,此修改對每個用戶都生效。
/etc/bashrc(ubuntu為 /etc/bash.bashrc)
為每一個運行 bash shell 的用戶執(zhí)行此文件。當(dāng) bash shell 被打開時,該文件被讀取。如果你想對所有的使用 bash 的用戶修改某個配置并在以后打開的 bash 都生效的話可以修改這個文件,修改這個文件不用重啟,重新打開一個 bash 即可生效。
Ubuntu沒有此文件,與之對應(yīng)的是/ect/bash.bashrc。
~/.bash_profile(ubuntu為 ~/.profile)
每個用戶都可使用該文件輸入專用于自己使用的 shell 信息,當(dāng)用戶登錄時,該文件僅僅執(zhí)行一次!默認(rèn)情況下,它設(shè)置一些環(huán)境變量,執(zhí)行用戶的~/ .bashrc 文件。 此文件類似于 /etc/profile,也是需要需要 source 才會生效,/etc/profile 對所有用戶生效,~/.bash_profile 只對當(dāng)前用戶生效。~/.profile(由Bourne Shell和Korn Shell使用)和.login(由C Shell使用)兩個文件是.bash_profile的同義詞,目的是為了兼容其它Shell。
- Linux的Shell種類眾多,常見的有:
Bourne Shell(/usr/bin/sh或/bin/sh)、
Bourne Again Shell(/bin/bash)、
C Shell(/usr/bin/csh)、
K Shell(/usr/bin/ksh)、
Shell for Root(/sbin/sh)等等。- 不同的Shell語言的語法有所不同,所以不能交換使用。每種Shell都有其特色之處,基本上,掌握其中任何一種 就足夠了。在本文中,我們關(guān)注的重點是Bash,也就是Bourne Again Shell,由于易用和免費,Bash在日常工作中被廣泛使用;同時,Bash也是大多數(shù)Linux系統(tǒng)默認(rèn)的Shell。
- 在一般情況下,人們并不區(qū)分 Bourne Shell和Bourne Again Shell,所以,在下面的文字中,我們可以看到#!/bin/sh,它同樣也可以改為#!/bin/bash。
~/.bashrc
該文件包含專用于你的 bash shell 的 bash 信息,當(dāng)?shù)卿洉r以及每次打開新的 shell 時,該文件被讀取。(每個用戶都有一個 ~/.bashrc 文件,在用戶目錄下) 此文件類似于 /etc/bashrc,不需要重啟就可以生效,重新打開一個 bash 即可生效,/etc/bashrc 對所有用戶新打開的 bash 都生效,但 ~/.bashrc 只對當(dāng)前用戶新打開的 bash 生效。
~/.bashrc文件會在bash shell調(diào)用另一個bash shell時讀取,也就是在shell中再鍵入bash命令啟動一個新shell時就會去讀該文件。這樣可有效分離登錄和子shell所需的環(huán)境。但一般 來說都會在/.bash_profile里調(diào)用/.bashrc腳本以便統(tǒng)一配置用戶環(huán)境。
~/.bash_logout:
當(dāng)每次退出系統(tǒng)(退出 bash shell)時,執(zhí)行該文件??砂岩恍┣謇砉ぷ鞯拿罘诺竭@文件中。
二、區(qū)別和聯(lián)系
- 在 /etc目錄是系統(tǒng)級(全局)的配置文件,當(dāng)在用戶主目錄下找不到~/.bash_profile 和~/.bashrc時,就會讀取這兩個文件。
- /etc/profile 中設(shè)定的變量(全局)的可以作用于任何用戶,而 ~/.bashrc 中設(shè)定的變量(局部)只能繼承 /etc/profile 中的變量,他們是“父子”關(guān)系。
- ~/.bash_profile 是交互式、login 方式進(jìn)入 bash 運行的; ~/.bashrc 是交互式 non-login 方式進(jìn)入 bash 運行的。通常二者設(shè)置大致相同,所以通常前者會調(diào)用后者。設(shè)置生效:可以重啟生效,也可以使用命令:source。
- ~/.bash_history是bash shell的歷史記錄文件,里面記錄了你在bash shell中輸入的所有命令??赏ㄟ^HISSIZE環(huán)境變量設(shè)置在歷史記錄文件里保存記錄的條數(shù)。
三、執(zhí)行順序
關(guān)于登錄linux時,/etc/profile、~/.bash_profile等幾個文件的執(zhí)行過程。
①在剛登錄Linux時,
- 首先啟動 /etc/profile 文件,
- 然后再啟動用戶目錄下的 ~/.bash_profile、 ~/.bash_login或 ~/.profile文件中
的其中一個,執(zhí)行的順序為:~/.bash_profile、 ~/.bash_login、 ~/.profile
其中~/.bash_profile、 ~/.bash_login和 ~/.profile文件往往只存在一個,這與Linux的發(fā)行版本有關(guān)。centos中為 ~/.bash_profile,ubuntu則為 ~/.profile。
- 以上兩個文件會在用戶登錄時執(zhí)行。
②下面開始執(zhí)行用戶的bash設(shè)置
如果 ~/.bash_profile文件存在的話,一般會以這樣的方式執(zhí)行用戶的 ~/.bashrc文件。
在 ~/.bash_profile文件中一般會有下面的代碼:
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
同樣~/.bashrc中,一般還會在文件的前面有以下代碼,來執(zhí)行/etc/bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
所以,~/.bashrc會調(diào)用 /etc/bashrc文件。最后,在退出shell時,還會執(zhí)行 ~/.bash_logout文件。
執(zhí)行順序為:
- /etc/profile
- ~/.bash_profile | ~/.bash_login | ~/.profile
- ~/.bashrc
- /etc/bashrc
- ~/.bash_logout
四、其他
- 圖形模式登錄時,順序讀?。?etc/profile和~/.profile
- 圖形模式登錄后,打開終端時,順序讀取:/etc/bash.bashrc和~/.bashrc
- 文本模式登錄時,順序讀?。?etc/bash.bashrc,/etc/profile和~/.bash_profile
- 從其它用戶su到該用戶,則分兩種情況:
(1)如果帶-l參數(shù)(或-參數(shù),–login參數(shù)),如:su -l username,則bash是lonin的,它將順序讀取以下配置文件:/etc/bash.bashrc,/etc/profile和~ /.bash_profile。
(2)如果沒有帶-l參數(shù),則bash是non-login的,它將順序讀?。?etc/bash.bashrc和~/.bashrc - 注銷時,或退出su登錄的用戶,如果是longin方式,那么bash會讀取:~/.bash_logout
- 執(zhí)行自定義的shell文件時,若使用“bash -l a.sh”的方式,則bash會讀取行:/etc/profile和~/.bash_profile,若使用其它方式,如:bash a.sh, ./a.sh,sh a.sh(這個不屬于bash shell),則不會讀取上面的任何文件。
參考博客:
Linux中profile、bashrc、/.bash_profile、/.bashrc、~/.bash_profile之間的區(qū)別和聯(lián)系以及執(zhí)行順序
淺析linux下的/etc/profile、/etc/bashrc、/.bash_profile、/.bashrc文件
Linux 中 profile,bashrc,bash_profile,/etc/profile,/etc/profile.d/的區(qū)別