首頁 運維 linux運維 Linux下關於環境變數如何設定的步驟分享(圖)

Linux下關於環境變數如何設定的步驟分享(圖)

Jul 24, 2017 pm 02:59 PM
linux 變數 如何

Linux中環境變數包含系統級和使用者級,系統級的環境變數是每個登入系統的使用者都要讀取的系統變量,而使用者級的環境變數則是該使用者使用系統時載入的環境變數。所以以下這篇文章主要要跟大家介紹了Linux中環境變數配置的相關資料,需要的朋友可以參考下。

簡介

我們大家平時使用Linux的時候,常常需要設定一些環境變量,這時候通常都是網路隨便搜搜就有人介紹經驗的。不過問題在於他們的方法各不相同,有人說配置在/etc/profile裡,有人說配置在/etc/environment,有人說配置在~/.bash_profile裡,有人說配置在~/.bashrc裡,有人說配置在~/.bash_login裡,有人說配置在~/.profile裡。 。 。這真是公說公有理。 。 。那麼問題來了,Linux到底是怎麼讀取設定檔的呢,依據又是什麼呢?下面這篇文章就來給大家詳細的介紹下,一起來看看吧。

文件

我一向討厭那種說結論不說出處的行為,這會給人一種「我憑什麼相信你」的感覺。而且事實上沒有出處就隨便議論得出的結論也基本上是人云亦雲的。事實上,與其去問別人,不如去問文檔。 找了一會,發現關於環境變數配置的相關文件其實是在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檔案為什麼重新登陸後設定不生效呢?這是因為我們的系統可能存在了前面兩個檔案中的一個,導致不會繼續讀取剩下的檔案。

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

 

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下關於環境變數如何設定的步驟分享(圖)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它們
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

centos和ubuntu的區別 centos和ubuntu的區別 Apr 14, 2025 pm 09:09 PM

CentOS 和 Ubuntu 的關鍵差異在於:起源(CentOS 源自 Red Hat,面向企業;Ubuntu 源自 Debian,面向個人)、包管理(CentOS 使用 yum,注重穩定;Ubuntu 使用 apt,更新頻率高)、支持週期(CentOS 提供 10 年支持,Ubuntu 提供 5 年 LTS 支持)、社區支持(CentOS 側重穩定,Ubuntu 提供廣泛教程和文檔)、用途(CentOS 偏向服務器,Ubuntu 適用於服務器和桌面),其他差異包括安裝精簡度(CentOS 精

centos如何安裝 centos如何安裝 Apr 14, 2025 pm 09:03 PM

CentOS 安裝步驟:下載 ISO 映像並刻錄可引導媒體;啟動並選擇安裝源;選擇語言和鍵盤佈局;配置網絡;分區硬盤;設置系統時鐘;創建 root 用戶;選擇軟件包;開始安裝;安裝完成後重啟並從硬盤啟動。

Centos停止維護2024 Centos停止維護2024 Apr 14, 2025 pm 08:39 PM

CentOS將於2024年停止維護,原因是其上游發行版RHEL 8已停止維護。該停更將影響CentOS 8系統,使其無法繼續接收更新。用戶應規劃遷移,建議選項包括CentOS Stream、AlmaLinux和Rocky Linux,以保持系統安全和穩定。

Centos停止維護後的選擇 Centos停止維護後的選擇 Apr 14, 2025 pm 08:51 PM

CentOS 已停止維護,替代選擇包括:1. Rocky Linux(兼容性最佳);2. AlmaLinux(與 CentOS 兼容);3. Ubuntu Server(需要配置);4. Red Hat Enterprise Linux(商業版,付費許可);5. Oracle Linux(與 CentOS 和 RHEL 兼容)。在遷移時,考慮因素有:兼容性、可用性、支持、成本和社區支持。

docker原理詳解 docker原理詳解 Apr 14, 2025 pm 11:57 PM

Docker利用Linux內核特性,提供高效、隔離的應用運行環境。其工作原理如下:1. 鏡像作為只讀模板,包含運行應用所需的一切;2. 聯合文件系統(UnionFS)層疊多個文件系統,只存儲差異部分,節省空間並加快速度;3. 守護進程管理鏡像和容器,客戶端用於交互;4. Namespaces和cgroups實現容器隔離和資源限制;5. 多種網絡模式支持容器互聯。理解這些核心概念,才能更好地利用Docker。

centos停止維護後怎麼辦 centos停止維護後怎麼辦 Apr 14, 2025 pm 08:48 PM

CentOS 停止維護後,用戶可以採取以下措施應對:選擇兼容髮行版:如 AlmaLinux、Rocky Linux、CentOS Stream。遷移到商業發行版:如 Red Hat Enterprise Linux、Oracle Linux。升級到 CentOS 9 Stream:滾動發行版,提供最新技術。選擇其他 Linux 發行版:如 Ubuntu、Debian。評估容器、虛擬機或云平台等其他選項。

docker desktop怎麼用 docker desktop怎麼用 Apr 15, 2025 am 11:45 AM

如何使用 Docker Desktop? Docker Desktop 是一款工具,用於在本地機器上運行 Docker 容器。其使用步驟包括:1. 安裝 Docker Desktop;2. 啟動 Docker Desktop;3. 創建 Docker 鏡像(使用 Dockerfile);4. 構建 Docker 鏡像(使用 docker build);5. 運行 Docker 容器(使用 docker run)。

vscode需要什麼電腦配置 vscode需要什麼電腦配置 Apr 15, 2025 pm 09:48 PM

VS Code 系統要求:操作系統:Windows 10 及以上、macOS 10.12 及以上、Linux 發行版處理器:最低 1.6 GHz,推薦 2.0 GHz 及以上內存:最低 512 MB,推薦 4 GB 及以上存儲空間:最低 250 MB,推薦 1 GB 及以上其他要求:穩定網絡連接,Xorg/Wayland(Linux)

See all articles