這篇文章將為大家詳細(xì)講解有關(guān)Linux中環(huán)境變量配置的示例分析,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
文檔
我一向討厭那種說(shuō)結(jié)論不說(shuō)出處的行為,這會(huì)給人一種“我憑什么相信你”的感覺(jué)。而且事實(shí)上沒(méi)有出處就隨便議論得出的結(jié)論也基本上是人云亦云的。事實(shí)上,與其去問(wèn)別人,不如去問(wèn)文檔。 找了一會(huì),發(fā)現(xiàn)關(guān)于環(huán)境變量配置的相關(guān)文檔其實(shí)是在bash命令的man文檔里,畢竟我們常用的就是這個(gè)shell。
在$man bash里,我發(fā)現(xiàn)了下面的一段文字:
INVOCATION A login shell is one whose first character of argument zero is a -, or one started with the --login option. 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. 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 filenames as described below under Tilde Expan‐ sion in the EXPANSION section. When bash is invoked as an interactive login shell, or as a non-inter‐ active shell with the --login option, it first reads and executes com‐ mands 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. When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists. When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if these files exist. 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 /etc/bash.bashrc and ~/.bashrc. 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 com‐ mand 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.
通過(guò)這段文字,我們發(fā)現(xiàn)其實(shí)所謂的環(huán)境變量配置文件,就是在shell登陸的時(shí)候自動(dòng)加載的那些文件。不過(guò)他所定義的登陸卻分為兩種:
login shell登陸。
interactive shell登陸。
login shell 登陸
所謂的login shell登陸,實(shí)際上就是指需要輸入密碼的登陸。具體的說(shuō),包括開(kāi)機(jī)登陸、ssh登陸,或者是輸入bash --login這種“假裝自己輸入密碼登陸”的方式。 在這種登陸方式下,系統(tǒng)會(huì)先讀取/etc/profile文件,然后,系統(tǒng)會(huì)依次搜索~/.bash_profile、~/.bash_login、~/.profile 這三個(gè)文件,并運(yùn)行只其中第一個(gè)存在的文件。 尤其要注意到后三個(gè)文件的“邏輯或”的關(guān)系。很多情況下我們會(huì)發(fā)現(xiàn),明明已經(jīng)修改了~/.profile文件為什么重新登陸后配置不生效呢?這是因?yàn)槲覀兊南到y(tǒng)可能存在了前面兩個(gè)文件中的一個(gè),導(dǎo)致不會(huì)繼續(xù)讀取剩下的文件。
下面的三張圖很好的說(shuō)明了這個(gè)問(wèn)題:
interactive shell 登陸
所謂的interactive shell登陸,其實(shí)就是相對(duì)于login shell登陸而言的。我們平時(shí)在登陸后右鍵打開(kāi)終端、或者CTRL+ALT+T打開(kāi)終端都是interactive shell登陸。 在這種登陸方式下,系統(tǒng)會(huì)依次讀取/etc/bash.bashrc和~/.bashrc,并加以執(zhí)行。 通常情況下,~/.bashrc文件里會(huì)默認(rèn)記錄一些常量和一些別名,尤其是$PS1變量,這個(gè)變量決定著bash提示符的格式、樣式以及顏色等。
注意:
需要注意的是,這兩種登陸方式讀取的是不同的配置文件,而且互相之間沒(méi)有交集,因此當(dāng)我們需要配置環(huán)境變量時(shí),我們要根據(jù)自己的登陸方式將需要的變量配置到不同的文件里。 例如下面這個(gè)經(jīng)典的問(wèn)題。
典型問(wèn)題
環(huán)境配置文件配置異常的例子是,當(dāng)我用ssh登錄服務(wù)器的時(shí)候,發(fā)現(xiàn)提示符是這樣的:
bash-4.3$
沒(méi)錯(cuò),就像上面第三張圖片里的那個(gè)bash一樣,提示符非常奇怪,而且當(dāng)輸入ls時(shí)文件和文件夾的顏色也沒(méi)有區(qū)分。 這個(gè)問(wèn)題顯然是由于$PS1這個(gè)環(huán)境變量沒(méi)有配置,導(dǎo)致他用了默認(rèn)值,雖然查看.bashrc文件時(shí)發(fā)現(xiàn)有$PS1這個(gè)變量的定義。,但是由于ssh屬于login shell,因此他在登陸時(shí)讀入的配置文件是/etc/profile一類(lèi)的文件,并沒(méi)有讀入.bashrc。 導(dǎo)致這個(gè)問(wèn)題的原因通常是我們誤刪除了/etc/profile里默認(rèn)的配置文件,因此解決的辦法也很簡(jiǎn)單。。。把.bashrc里的部分文件復(fù)制到/etc/profile里就行了。
這個(gè)問(wèn)題給我們的啟示是,當(dāng)我們?yōu)榉?wù)器配置變量時(shí),盡量配置到/etc/profile里或者~/.bash_profile里,因?yàn)橛胹sh登錄服務(wù)器是基本上用不到.bashrc文件的;當(dāng)我們給自己的電腦配置環(huán)境變量時(shí),盡量配置到.bashrc里,因?yàn)檫@樣我們只要打開(kāi)終端就會(huì)讀入這個(gè)文件,這樣就可以不用注銷(xiāo)就能應(yīng)用配置了(只有注銷(xiāo)重新登錄才會(huì)應(yīng)用/etc/profile一類(lèi)的配置文件)。
關(guān)于“Linux中環(huán)境變量配置的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
新聞名稱(chēng):Linux中環(huán)境變量配置的示例分析-創(chuàng)新互聯(lián)
轉(zhuǎn)載源于:http://aaarwkj.com/article14/dshoge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、網(wǎng)站制作、小程序開(kāi)發(fā)、靜態(tài)網(wǎng)站、定制開(kāi)發(fā)、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容