Shell環境變量以及set,env,export的區別

shell環境變量的分類以及set env export的區別:

set:顯示(設置)shell變量 包括的私有變量以及用戶變量,不一樣類的shell有不一樣的私有變量 bash,ksh,csh每中shell私有變量都不同html

env:顯示(設置)用戶變量變量linux

export:顯示(設置)當前導出成用戶變量的shell變量。shell

舉個例子來說:bash

[www.linuxidc.com@linuxidc ~]$ aaa=bbb --shell變量設定
[www.linuxidc.com@linuxidc ~]$ echo $aaa
bbb
[www.linuxidc.com@linuxidc ~]$ env| grep aaa --設置完當前用戶變量並無
[www.linuxidc.com@linuxidc ~]$ set| grep aaa --shell變量有
aaa=bbb
[www.linuxidc.com@linuxidc ~]$ export| grep aaa --這個指的export也沒導出,導出變量也沒有
[www.linuxidc.com@linuxidc ~]$ export aaa --那麼用export 導出一下
[www.linuxidc.com@linuxidc ~]$ env| grep aaa --發現用戶變量內存在了
aaa=bbb
總結:linux 分 shell變量(set),用戶變量(env), shell變量包含用戶變量,export是一種命令工具,是顯示那些經過export命令把shell變量中包含的用戶變量導入給用戶變量的那些變量.工具

環境變量的配置文件

最根本的設置、更改變量的配置文件 ~/.bash_profile ~/.bashrc ~/.bash_logouthtm

~/.bash_profile 用戶登陸時被讀取,其中包含的命令被執行內存

~/.bashrc 啓動新的shell時被讀取,並執行get

~/.bash_logout shell 登陸退出時被讀取it

.bashrc和.bash_profile的區別

引自io

.bash_profile會用在login shell .bashrc 使用在interactive non-login shell

Bash下每一個用戶均可以配置兩個初始文件:.bash_profile和.bashrc,文件存儲在~根目錄中。man bash中的相關解釋以下:

,---------------------------------------------------------------------------- | ~/.bash_profile | The personal initialization file, executed for login shells | ~/.bashrc | The individual per-interactive-shell startup file `----------------------------------------------------------------------------

  • 每次bash做爲login shell啓動時會執行.bash_profile。

  • 每次bash做爲普通的交互shell(interactive shell)啓動時會執行.bashrc

  • 注意 1)在shell腳本中「#!/usr/bin/bash」啓動的bash並不執行.bashrc。由於這裏的bash不是interactive shell。

2)bash做爲login shell(login bash)啓動時並不執行.bashrc。雖然該shell也是interactive shell,但它不是普通的shell。

  • 通常.bash_profile裏都會調用.bashrc

儘管login bash啓動時不會自動執行.bashrc,慣例上會在.bash_profile中顯式調用.bashrc。因此在你的.bash_profile文件中,極可能會看到以下的代碼片斷: [plain] view plaincopy在CODE上查看代碼片派生到個人代碼片 if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

.bashrc 使用在interactive non-login shell。意思是你每次運行一個bash腳本的話,.bashrc就會被執行一次。有個簡單的方法,你在.bash_profile和.bashrc裏 都用echo打印點東西。,就能夠看到着兩個文件都是何時被執行的了。

編輯/etc/profile修改全局環境變量 編輯.bash_profile修改當前用戶的環境變量 修改完成以後source一下便可生效,例如source ~/.bash_profile

ps: 常會看到xxxrc ——以「rc」結尾的配置文件。「rc」好像是「Runtime Command」的意思。

相關文章
相關標籤/搜索