export 命令用于设置或显示环境变量。设置效力仅限于此次登录。
语法:
1
export [-fnp][变量名]=[变量值]
定义环境变量
只声明环境变量:
1
2
3
[root@centos uphie]# export MYENV1
[root@centos uphie]# export -p | grep MYENV
declare -x MYENV1
声明环境变量并赋值:
1
2
3
4
[root@centos uphie]# export MYENV2=uphie.studio
[root@centos uphie]# export -p | grep MYENV
declare -x MYENV1
declare -x MYENV2="uphie.studio"
选项
-f
代表 [变量名]
中为函数名。
1
2
3
4
5
6
7
8
9
10
11
[root@centos uphie]# function txtCnt(){ echo `ls *.txt|wc -l`;}
[root@centos uphie]# txtCnt
7
[root@centos uphie]# vim test.sh
#!/bin/bash
txtCnt
[root@centos uphie]# bash ./test.sh
./test.sh:行2: txtCnt: 未找到命令
[root@centos uphie]# export -f txtCnt
[root@centos uphie]# bash ./test.sh
7
-n
删除指定的变量,不会输出到后续指令的执行环境中。
1
2
3
4
5
6
[root@centos uphie]# export -p | grep MYENV
declare -x MYENV1
declare -x MYENV2="uphie.studio"
[root@centos uphie]# export -n MYENV1
[root@centos uphie]# export -p | grep MYENV
declare -x MYENV2="uphie.studio"
-p
列出所有的 Shell 赋予程序的环境变量。
1
2
3
4
5
6
[root@centos uphie]# export -p | head -5
declare -x HISTCONTROL="ignoredups"
declare -x HISTSIZE="1000"
declare -x HOME="/root"
declare -x HOSTNAME="centos"
declare -x LANG="zh_CN.UTF-8"