中文字幕第五页-中文字幕第页-中文字幕韩国-中文字幕最新-国产尤物二区三区在线观看-国产尤物福利视频一区二区

Linux中環境變量配置的示例分析-創新互聯

這篇文章將為大家詳細講解有關Linux中環境變量配置的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

創新互聯建站專業為企業提供西鄉網站建設、西鄉做網站、西鄉網站設計、西鄉網站制作等企業網站建設、網頁設計與制作、西鄉企業網站模板建站服務,10年西鄉做網站經驗,不只是建網站,更提供有價值的思路和整體網絡服務。

文檔

我一向討厭那種說結論不說出處的行為,這會給人一種“我憑什么相信你”的感覺。而且事實上沒有出處就隨便議論得出的結論也基本上是人云亦云的。事實上,與其去問別人,不如去問文檔。 找了一會,發現關于環境變量配置的相關文檔其實是在bash命令的man文檔里,畢竟我們常用的就是這個shell。

在$man bash里,我發現了下面的一段文字:

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.

通過這段文字,我們發現其實所謂的環境變量配置文件,就是在shell登陸的時候自動加載的那些文件。不過他所定義的登陸卻分為兩種:

  • login shell登陸。

  • interactive shell登陸。

login shell 登陸

所謂的login shell登陸,實際上就是指需要輸入密碼的登陸。具體的說,包括開機登陸、ssh登陸,或者是輸入bash --login這種“假裝自己輸入密碼登陸”的方式。 在這種登陸方式下,系統會先讀取/etc/profile文件,然后,系統會依次搜索~/.bash_profile、~/.bash_login、~/.profile 這三個文件,并運行只其中第一個存在的文件。 尤其要注意到后三個文件的“邏輯或”的關系。很多情況下我們會發現,明明已經修改了~/.profile文件為什么重新登陸后配置不生效呢?這是因為我們的系統可能存在了前面兩個文件中的一個,導致不會繼續讀取剩下的文件。

下面的三張圖很好的說明了這個問題:

Linux中環境變量配置的示例分析

Linux中環境變量配置的示例分析

Linux中環境變量配置的示例分析

interactive shell 登陸

所謂的interactive shell登陸,其實就是相對于login shell登陸而言的。我們平時在登陸后右鍵打開終端、或者CTRL+ALT+T打開終端都是interactive shell登陸。 在這種登陸方式下,系統會依次讀取/etc/bash.bashrc和~/.bashrc,并加以執行。 通常情況下,~/.bashrc文件里會默認記錄一些常量和一些別名,尤其是$PS1變量,這個變量決定著bash提示符的格式、樣式以及顏色等。

注意:

需要注意的是,這兩種登陸方式讀取的是不同的配置文件,而且互相之間沒有交集,因此當我們需要配置環境變量時,我們要根據自己的登陸方式將需要的變量配置到不同的文件里。 例如下面這個經典的問題。

典型問題

環境配置文件配置異常的例子是,當我用ssh登錄服務器的時候,發現提示符是這樣的:

bash-4.3$

沒錯,就像上面第三張圖片里的那個bash一樣,提示符非常奇怪,而且當輸入ls時文件和文件夾的顏色也沒有區分。 這個問題顯然是由于$PS1這個環境變量沒有配置,導致他用了默認值,雖然查看.bashrc文件時發現有$PS1這個變量的定義。,但是由于ssh屬于login shell,因此他在登陸時讀入的配置文件是/etc/profile一類的文件,并沒有讀入.bashrc。 導致這個問題的原因通常是我們誤刪除了/etc/profile里默認的配置文件,因此解決的辦法也很簡單。。。把.bashrc里的部分文件復制到/etc/profile里就行了。

這個問題給我們的啟示是,當我們為服務器配置變量時,盡量配置到/etc/profile里或者~/.bash_profile里,因為用ssh登錄服務器是基本上用不到.bashrc文件的;當我們給自己的電腦配置環境變量時,盡量配置到.bashrc里,因為這樣我們只要打開終端就會讀入這個文件,這樣就可以不用注銷就能應用配置了(只有注銷重新登錄才會應用/etc/profile一類的配置文件)。

關于“Linux中環境變量配置的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

新聞名稱:Linux中環境變量配置的示例分析-創新互聯
轉載源于:http://www.2m8n56k.cn/article14/dshoge.html

成都網站建設公司_創新互聯,為您提供面包屑導航網站制作小程序開發靜態網站定制開發做網站

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:[email protected]。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

成都網站建設
主站蜘蛛池模板: 美女黄视频免费观看 | 好吊操这里只有精品 | 国产在线视频精品视频免费看 | 精品国产一区二区三区免费看 | 国产精品线在线精品国语 | 伊人久久91| 日日狠狠久久偷偷四色综合免费 | 亚洲欧美不卡中文字幕 | 久久好看视频 | 久久精品视频一区二区三区 | 草久久免费视频 | 久久久国产99久久国产一 | 欧美另类极品 | 亚洲国产成人久久综合一区 | 高清精品女厕在线观看 | 欧美理论大片清免费观看 | 亚洲国产欧美另类 | 亚州成人 | 波多野结衣中文无毒不卡 | 欧美久久久久 | 黄大片日本一级在线a | 亚洲一区免费看 | 日韩 国产 在线 | 国产91精品一区二区视色 | 手机看片精品国产福利盒子 | 国产亚洲精品线观看77 | 777色狠狠一区二区三区 | 久久久久久91精品色婷婷 | 女人张开双腿让男人桶爽免 | 日韩一级欧美一级毛片在 | 肥婆毛片| 欧美精品hdvdeosex4k | 亚洲欧美中文字幕在线网站 | 国产麻豆交换夫妇 | 国产成人丝袜网站在线看 | 亚洲清纯自偷自拍另类专区 | a毛片全部播放免费视频完整18 | 黄影院| 亚洲欧美天堂 | 国产三级网 | 亚洲精品视|