linux export命令(export指令)source命令(source指令)

export 命令

参考文档

Linux export命令 | 菜鸟教程

Linux export命令用于设置或显示环境变量。

在shell中执行程序时,shell会提供一组环境变量。export可新增,修改或删除环境变量,供后续执行的程序使用。export的效力仅及于该次登陆操作。

语法

export [-fnp][变量名称]=[变量设置值]
-f  代表[变量名称]中为函数名称。
-n  删除指定的变量。变量实际上并未删除,只是不会输出到后续指令的执行环境中。
-p  列出所有的shell赋予程序的环境变量。
1. 输出系统环境变量(export -p)
[root@master ~]# export -p
declare -x HISTCONTROL="ignoredups"
declare -x HISTSIZE="1000"
declare -x HOME="/root"
declare -x HOSTNAME="master"
declare -x LANG="en_US.UTF-8"
declare -x LESSOPEN="||/usr/bin/lesspipe.sh %s"
declare -x LOGNAME="root"
declare -x LS_COLORS="rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:"
declare -x MAIL="/var/spool/mail/root"
declare -x OLDPWD
declare -x PATH="/opt/k8s/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
declare -x PWD="/root"
declare -x SHELL="/bin/bash"
declare -x SHLVL="1"
declare -x SSH_AUTH_SOCK="/tmp/ssh-IOBZiV56pn/agent.4255"
declare -x SSH_CLIENT="192.168.63.200 50323 22"
declare -x SSH_CONNECTION="192.168.63.200 50323 192.168.63.120 22"
declare -x SSH_TTY="/dev/pts/0"
declare -x TERM="vt100"
declare -x USER="root"
declare -x XDG_RUNTIME_DIR="/run/user/0"
declare -x XDG_SESSION_ID="1"
2. 添加一个环境变量
[root@master ~]# export AA=2
[root@master ~]# export -p | grep AA
declare -x AA="2"
[root@master ~]# 
3. 更改变量的值
[root@master ~]# AA=3
[root@master ~]# export -p | grep AA
declare -x AA="3"
[root@master ~]# export AA=4
[root@master ~]# export -p | grep AA
declare -x AA="4"
[root@master ~]# 
4. 删除环境变量(无论变量是只存在该shell中还是环境变量)(unset)

[root@master ~]# unset AA
[root@master ~]# export -p | grep AA
[root@master ~]#
[root@master ~]#

5. 从文件中添加环境变量
[root@master ~]# ll test.sh 
-rw-r--r-- 1 root root 23 Sep  2 23:24 test.sh
[root@master ~]# cat test.sh 
export AAA=1
echo $AAA
[root@master ~]# source test.sh 
1
[root@master ~]# export -p | grep AA
declare -x AA="4"
declare -x AAA="1"
[root@master ~]# 

source 命令

参考文档

linux source命令 - 电院院长 - 博客园

1. 在具有可执行权限脚本中,./sh 作用一致,都是执行该脚本,并且是在子shell中的,脚本中使用到的变量不会影响父shell linux source命令
[root@master ~]# ll test.sh 
-rwxr-xr-x 1 root root 16 Sep  2 23:06 test.sh
[root@master ~]# cat test.sh 
AAA=1
echo $AAA
[root@master ~]# sh test.sh 
1
[root@master ~]# echo $AAA

[root@master ~]# ./test.sh 
1
[root@master ~]# echo $AAA

[root@master ~]# 

而source 则是使脚本中的变量保存在该shell中。

[root@master ~]# source test.sh 
1
[root@master ~]# echo $AAA
1
[root@master ~]# unset AAA          #在父shell中取消该变量
[root@master ~]# echo $AAA

[root@master ~]# 
[root@master ~]# 
2. 如果该文件没有运行权限,./ 无法运行,sh与有运行权限一致,在子shell中运行脚本但不把变量传递给父shell, source与有运行权限一致,执行脚本并把变量传递给父shell
[root@master ~]# chmod -x test.sh 
[root@master ~]# ll test.sh 
-rw-r--r-- 1 root root 16 Sep  2 23:06 test.sh
[root@master ~]# ./test.sh
-bash: ./test.sh: Permission denied
[root@master ~]# sh test.sh
1
[root@master ~]# echo $AAA

[root@master ~]# source test.sh 
1
[root@master ~]# echo $AAA
1
[root@master ~]# 
[root@master ~]# export -p | grep AAA
[root@master ~]# 
3. 如果想要把环境变量添加到系统变量中,则只能通过source指令在脚本中使用export来实现
[root@master ~]# cat test.sh 
export AAA=1
echo $AAA
[root@master ~]# export -p | grep AAA
[root@master ~]# sh test.sh 
1
[root@master ~]# export -p | grep AAA
[root@master ~]# chmod +x test.sh 
[root@master ~]# sh test.sh 
1
[root@master ~]# export -p | grep AAA
[root@master ~]# ./test.sh 
1
[root@master ~]# export -p | grep AAA
[root@master ~]# source test.sh 
1
[root@master ~]# export -p | grep AAA
declare -x AAA="1"
[root@master ~]# 
[root@master ~]# sh
sh-4.2# export -p | grep AAA
export AAA="1"
sh-4.2# echo $AAA
1
sh-4.2# 

总结:

sh无论脚本有无运行权限,都可以在子shell中运行脚本,脚本变量不会保存到父shell和系统变量中

./只能运行有运行权限的脚本,并且只能在子shell中使用变量,不会传递给父shell和系统变量

source 无论脚本有无运行权限,都可以逐行执行脚本,并且把脚本中的变量传递给父shell,如果变量使用了export,则会传递给系统变量

参考文章:Linux中export和source的使用方法

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值