失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > linux export命令(export指令)source命令(source指令)

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

时间:2019-09-04 13:09:30

相关推荐

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

文章目录

export 命令参考文档语法1. 输出系统环境变量(export -p)2. 添加一个环境变量3. 更改变量的值4. 删除环境变量(无论变量是只存在该shell中还是环境变量)(unset)5. 从文件中添加环境变量 source 命令参考文档1. 在具有可执行权限脚本中,`./` 和 `sh` 作用一致,都是执行该脚本,并且是在子shell中的,脚本中使用到的变量不会影响父shell linux source命令2. 如果该文件没有运行权限,`./` 无法运行,sh与有运行权限一致,在子shell中运行脚本但不把变量传递给父shell, source与有运行权限一致,执行脚本并把变量传递给父shell3. 如果想要把环境变量添加到系统变量中,则只能通过source指令在脚本中使用export来实现

export 命令

参考文档

Linux export命令 | 菜鸟教程

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

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

语法

export [-fnp][变量名称]=[变量设置值]-f 代表[变量名称]中为函数名称。-n 删除指定的变量。变量实际上并未删除,只是不会输出到后续指令的执行环境中。-p 列出所有的shell赋予程序的环境变量。

1. 输出系统环境变量(export -p)

[root@master ~]# export -pdeclare -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 OLDPWDdeclare -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 AAdeclare -x AA="2"[root@master ~]#

3. 更改变量的值

[root@master ~]# AA=3[root@master ~]# export -p | grep AAdeclare -x AA="3"[root@master ~]# export AA=4[root@master ~]# export -p | grep AAdeclare -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=1echo $AAA[root@master ~]# source test.sh 1[root@master ~]# export -p | grep AAdeclare -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=1echo $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 $AAA1[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.sh1[root@master ~]# echo $AAA[root@master ~]# source test.sh 1[root@master ~]# echo $AAA1[root@master ~]# [root@master ~]# export -p | grep AAA[root@master ~]#

3. 如果想要把环境变量添加到系统变量中,则只能通过source指令在脚本中使用export来实现

[root@master ~]# cat test.sh export AAA=1echo $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 AAAdeclare -x AAA="1"[root@master ~]# [root@master ~]# shsh-4.2# export -p | grep AAAexport AAA="1"sh-4.2# echo $AAA1sh-4.2#

总结:

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

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

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

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

如果觉得《linux export命令(export指令)source命令(source指令)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。