linux 系统中 获取环境变量、 获取环境变量+自定义变量(Get environment variables, get environment variables + custom variables in Linux system)

1、获取环境变量

export

env

2、测试export 和 env:

root@PC1:/home/test# ls
root@PC1:/home/test# export > export.txt  ## 生成文件
root@PC1:/home/test# ls
export.txt
root@PC1:/home/test# env > env.txt   ## 生成文件
root@PC1:/home/test# ls
env.txt  export.txt
root@PC1:/home/test# head env.txt
SHELL=/bin/bash
window=100000
LC_ADDRESS=zh_CN.UTF-8
LC_NAME=zh_CN.UTF-8
LC_MONETARY=zh_CN.UTF-8
PWD=/home/test
LOGNAME=root
XDG_SESSION_TYPE=tty
MOTD_SHOWN=pam
HOME=/root
root@PC1:/home/test# head export.txt
declare -x DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/0/bus"
declare -x DISPLAY=":0.0"
declare -x HOME="/root"
declare -x LANG="en_US.UTF-8"
declare -x LC_ADDRESS="zh_CN.UTF-8"
declare -x LC_IDENTIFICATION="zh_CN.UTF-8"
declare -x LC_MEASUREMENT="zh_CN.UTF-8"
declare -x LC_MONETARY="zh_CN.UTF-8"
declare -x LC_NAME="zh_CN.UTF-8"
declare -x LC_NUMERIC="zh_CN.UTF-8"
root@PC1:/home/test# awk '{print $3}' export.txt | sed 's/"//g' | head
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/0/bus
DISPLAY=:0.0
HOME=/root
LANG=en_US.UTF-8
LC_ADDRESS=zh_CN.UTF-8
LC_IDENTIFICATION=zh_CN.UTF-8
LC_MEASUREMENT=zh_CN.UTF-8
LC_MONETARY=zh_CN.UTF-8
LC_NAME=zh_CN.UTF-8
LC_NUMERIC=zh_CN.UTF-8
root@PC1:/home/test# awk '{print $3}' export.txt | sed 's/"//g' | sort > export2.txt  ## 保留两个文件的重合部分,排序
root@PC1:/home/test# sort env.txt env2.txt
sort: cannot read: env2.txt: No such file or directory
root@PC1:/home/test# sort env.txt > env2.txt  ##排序
root@PC1:/home/test# diff export2.txt env2.txt  ## 比较,发现两个变量实质内容是一样的,说明env和export都是生成环境变量的
14,15c14,15
< LESSCLOSE=/usr/bin/lesspipe
< LESSOPEN=|
---
> LESSCLOSE=/usr/bin/lesspipe %s %s
> LESSOPEN=| /usr/bin/lesspipe %s
25,26c25,26
< SSH_CLIENT=192.168.3.4
< SSH_CONNECTION=192.168.3.4
---
> SSH_CLIENT=192.168.3.4 1392 22
> SSH_CONNECTION=192.168.3.4 1392 192.168.3.106 22
29a30
> _=/usr/bin/env
root@PC1:/home/test#

3、declare 和 set获取环境变量和自定义变量

测试:

root@PC1:/home/test# ls
root@PC1:/home/test# declare > declare.txt
root@PC1:/home/test# ls
declare.txt
root@PC1:/home/test# set > set.txt
root@PC1:/home/test# ls
declare.txt  set.txt
root@PC1:/home/test# diff declare.txt set.txt  ## 两者是一样的
root@PC1:/home/test# wc -l *
  1684 declare.txt
  1684 set.txt
  3368 total

4、自定义变量和 export 处理变量后的区别

export处理后的变量是环境变量,相当于全局变量,在自进程中可见。

自定变量相当于局部变量,仅在当前环境起作用

测试:

root@PC1:/home/test# ls
root@PC1:/home/test# test_env=100   ##自定义变量
root@PC1:/home/test# export | grep "test_env"  ## 在环境变量中无法检索到
root@PC1:/home/test# declare | grep "test_env"  ## 可以在自定义变量中检索到
test_env=100
root@PC1:/home/test# export test_env2=200     ## 用export处理自定义变量
root@PC1:/home/test# export | grep "test_env2"  ## 可以在环境变量中检索到,说明export的作用是将自定义变量升级为环境变量
declare -x test_env2="200"
root@PC1:/home/test# declare | grep "test_env2"  ## 也可以在自定义变量检索到
_=test_env2=200
test_env2=200

参考:http://c.biancheng.net/linux/export.html

————————

1. Get environment variables

export

env

2. Test export and env:

root@PC1:/home/test# ls
root@PC1:/home/test# export > export.txt  ## 生成文件
root@PC1:/home/test# ls
export.txt
root@PC1:/home/test# env > env.txt   ## 生成文件
root@PC1:/home/test# ls
env.txt  export.txt
root@PC1:/home/test# head env.txt
SHELL=/bin/bash
window=100000
LC_ADDRESS=zh_CN.UTF-8
LC_NAME=zh_CN.UTF-8
LC_MONETARY=zh_CN.UTF-8
PWD=/home/test
LOGNAME=root
XDG_SESSION_TYPE=tty
MOTD_SHOWN=pam
HOME=/root
root@PC1:/home/test# head export.txt
declare -x DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/0/bus"
declare -x DISPLAY=":0.0"
declare -x HOME="/root"
declare -x LANG="en_US.UTF-8"
declare -x LC_ADDRESS="zh_CN.UTF-8"
declare -x LC_IDENTIFICATION="zh_CN.UTF-8"
declare -x LC_MEASUREMENT="zh_CN.UTF-8"
declare -x LC_MONETARY="zh_CN.UTF-8"
declare -x LC_NAME="zh_CN.UTF-8"
declare -x LC_NUMERIC="zh_CN.UTF-8"
root@PC1:/home/test# awk '{print $3}' export.txt | sed 's/"//g' | head
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/0/bus
DISPLAY=:0.0
HOME=/root
LANG=en_US.UTF-8
LC_ADDRESS=zh_CN.UTF-8
LC_IDENTIFICATION=zh_CN.UTF-8
LC_MEASUREMENT=zh_CN.UTF-8
LC_MONETARY=zh_CN.UTF-8
LC_NAME=zh_CN.UTF-8
LC_NUMERIC=zh_CN.UTF-8
root@PC1:/home/test# awk '{print $3}' export.txt | sed 's/"//g' | sort > export2.txt  ## 保留两个文件的重合部分,排序
root@PC1:/home/test# sort env.txt env2.txt
sort: cannot read: env2.txt: No such file or directory
root@PC1:/home/test# sort env.txt > env2.txt  ##排序
root@PC1:/home/test# diff export2.txt env2.txt  ## 比较,发现两个变量实质内容是一样的,说明env和export都是生成环境变量的
14,15c14,15
< LESSCLOSE=/usr/bin/lesspipe
< LESSOPEN=|
---
> LESSCLOSE=/usr/bin/lesspipe %s %s
> LESSOPEN=| /usr/bin/lesspipe %s
25,26c25,26
< SSH_CLIENT=192.168.3.4
< SSH_CONNECTION=192.168.3.4
---
> SSH_CLIENT=192.168.3.4 1392 22
> SSH_CONNECTION=192.168.3.4 1392 192.168.3.106 22
29a30
> _=/usr/bin/env
root@PC1:/home/test#

3. Declare and set get environment variables and custom variables

Test:

root@PC1:/home/test# ls
root@PC1:/home/test# declare > declare.txt
root@PC1:/home/test# ls
declare.txt
root@PC1:/home/test# set > set.txt
root@PC1:/home/test# ls
declare.txt  set.txt
root@PC1:/home/test# diff declare.txt set.txt  ## 两者是一样的
root@PC1:/home/test# wc -l *
  1684 declare.txt
  1684 set.txt
  3368 total

4. Differences between custom variables and export variables

The variables processed by export are environment variables, which are equivalent to global variables and are visible in the self process.

A custom variable is equivalent to a local variable and works only in the current environment

Test:

root@PC1:/home/test# ls
root@PC1:/home/test# test_env=100   ##自定义变量
root@PC1:/home/test# export | grep "test_env"  ## 在环境变量中无法检索到
root@PC1:/home/test# declare | grep "test_env"  ## 可以在自定义变量中检索到
test_env=100
root@PC1:/home/test# export test_env2=200     ## 用export处理自定义变量
root@PC1:/home/test# export | grep "test_env2"  ## 可以在环境变量中检索到,说明export的作用是将自定义变量升级为环境变量
declare -x test_env2="200"
root@PC1:/home/test# declare | grep "test_env2"  ## 也可以在自定义变量检索到
_=test_env2=200
test_env2=200

参考:http://c.biancheng.net/linux/export.html