centos中/etc/profile,/etc/bashrc等檔案的區別與作用

來源:互聯網
上載者:User


當我們在做一些與bash相關的操作時,比如設定別名、登入啟動項等,多多少少都會與下面幾個檔案打交道,用的時候一查,然後又忘了。好記性不如爛筆頭,下面老高就幫你理一理這些檔案到底是幹啥的。

/etc/profile
/etc/bashrc
~/.bash_profile
~/.bashrc

•檔案說明:

◦/ect/profile
■此檔案為系統的每個使用者佈建環境資訊,當使用者第一次登入時,該檔案被執行.並從/etc/profile.d目錄的設定檔中搜集shell的設定.
◦/etc/bashrc
■為每一個運行bash shell的使用者執行此檔案.當bash shell被開啟時,該檔案被讀取.
◦~/.bash_profile
■每個使用者都可使用該檔案輸入專用於自己使用的shell資訊,當使用者登入時,該檔案僅僅執行一次!預設情況下,他設定一些環境變數,執行~/.bashrc檔案.
◦~/.bashrc
■該檔案包含專用於使用者的bash shell的bash資訊,當登入時以及每次開啟新的shell時,該該檔案被讀取.
◦~/.bash_logout
■當每次退出系統(退出bash shell)時,執行該檔案.


•另外, /etc/profile 中設定的變數(全域)的可以作用於任何使用者
•而~/.bashrc等中設定的變數(局部)只能繼承 /etc/profile 中的變數,他們是"父子"關係.


man bash ----- INVOCATION

以CENTOS為例,其他系統可能略有區別。

登入shell

A login shell is one whose first character of argument zero is a -, or one started with the --login option.
老高註解:

判斷一個shell是不是登入shell,只要你是su -, bash --login這樣進入的就行,如果你進入一個terminal而不需要登入,即輸入使用者名稱密碼,那麼他就是一個 non-login shell。比如你在MAC下開啟終端,是不需要登入過程的所以是non-login shell。

還有一個方法,echo $0,如果是登入shell,則會返回-[bash name],否則返回[bash name]。

互動式Shell

An interactive shell is one started without non-option arguments and without the -c option whose standard input and error are both connected to terminals (as determined by isatty(3)), or one started with the -i option. PS1 is set and $- includes i if bash is interactive,allowing a shell script or a startup file to test this state.
老高註解:

判斷一個互動式shell也很簡單echo $-的結果中包含小寫字母i就是了。

當你運行一個指令碼時,那就是一個非互動式shell。
When bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute. Bash behaves as if the following command were executed:
if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
but the value of the PATH variable is not used to search for the file name.
老高翻譯:

當一個非互動的shell運行一個shell指令碼時,他會在環境變數中 $BASH_ENV 並執行。代碼為if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi。

The following paragraphs describe how bash executes its startup files. If any of the files exist but cannot be read, bash reports an error. Tildes are expanded in file names as described below under Tilde Expansion in the EXPANSION section.
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
老高翻譯:

當你登入一個互動shell後,如果/etc/profile存在,bash首先會讀取/etc/profile的配置,然後再依次讀取對應使用者的~/.bash_profile、~/.bash_login和~/.profile。

When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists.
老高翻譯:

當一個登入shell退出的時候,如果目前使用者的~/.bash_logout存在,bash會讀取他。

When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.
老高翻譯:

當一個非登陸shell運行,如果對應使用者的~/.bashrc存在,bash首先會讀取~/.bashrc,有個--norc option能阻止讀取,還有個--rcfile的選項可以強制讀取指定檔案。

綜上:

~/.bashrc與/etc/bashrc、~/.bash_profile與/etc/profile的關係一樣,都是一個針對某個使用者,一個針對全域設定。讀取的時候也是全域優先。

互動式shell登入時讀取profile,初始化時讀取bashrc。

非互動式只會讀取bashrc。

一般把alias和function一類的放到bashrc或~/.bashrc中。

而把export更多的放在profile檔案中。

推薦大家參考My bashrc, bash aliases, profile and other files的配置;

同時安裝Linux下最好的shell :zsh + oh-my-zsh 預設shell的最佳替代品;

如果還是鐘情與bash,那麼老高推薦你安裝bash-completion,自動補全的功能很好用!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.