export [-fn] [name[=word]]...
export -p
shell
-f:指向函数。
-n:删除变量的导出属性。
-p:显示全部拥有导出属性的变量。
-pf:显示全部拥有导出属性的函数。
-nf:删除函数的导出属性。
--:在它之后的选项无效。
name(可选):变量名或已定义函数名。
value(可选):变量的值。
export返回true除非你提供了非法选项或非法名称。
shell
<h1>显示全部拥有导出属性的变量。</h1>
<h1>export -p</h1>
<h1>export</h1>
<h1>显示全部拥有导出属性的函数。</h1>
<h1>export -pf</h1>
shell
<h1>首先删除要演示的变量名</h1>
#unset a b
<h1>定义变量的同时增加导出属性</h1>
export a b=3
<h1>当然也可以先定义后增加导出属性</h1>
b=3
export b
<h1>修改拥有导出属性的变量的值</h1>
export a=5 b=7
<h1>当然也可以直接赋值修改</h1>
a=5;b=7
<h1>删除变量的导出属性</h1>
export -n a b
shell
<h1>首先删除要演示的函数名</h1>
unset func_1 func_2
<h1>创建函数</h1>
function func_1(){ echo '123'; }
function func_2(){ echo '890'; }
<h1>为已定义函数增加导出属性</h1>
export -f func_1 func_2
<h1>删除函数的导出属性</h1>
export -fn a b
shell <h1>添加环境变量(JAVA)到<code>~/.bashrcPATH=/usr/local/jdk1.7.0/bin:$PATH添加当前位置到动态库环境变量
export LD_LIBRARY_PATH=$(pwd):${LD_LIBRARY_PATH}
-- 后使用选项。
A:它们会成为环境变量,可以在脚本中访问它们,尤其是脚本中调用的子进程需要时。( [参考链接4][4] )
A:只有通过source方式调用的脚本会生效,您可以查看source命令获得更多信息;其他方式只是在子shell中执行。
之前的不会影响,之后的除非是修改了~/.bashrc这种启动终端时加载的脚本。( [参考链接1][1] )
~/.bashrc中定义的函数和变量。为什么在新打开的终端中通过 sh 方式调用该脚本或直接运行这个当前用户有执行权限的脚本却不能使用这些函数和变量?
A:请在~/.bashrc文件中增加export它们的语句。另请参阅 知识点 段落。
A:是可以的(如果你的bash支持它们),不过有些问题( [参考链接2][2] )。
declare? A:因为declare也能够设置变量或函数的导出属性,详见declare命令。
help命令。
在info bash或 [bash在线文档](http://www.gnu.org/software/bash/manual/bash.html) 的
3.7.3节提到了shell执行环境,其中涉及变量和函数的内容如下
> - shell parameters that are set by variable assignment or with set or inherited from the shell’s parent in the environment
> - shell functions defined during execution or inherited from the shell’s parent in the environment
那么第一句话中的参数又和变量有什么关系呢?在3.4节第一段中提到:
> A variable is a parameter denoted by a name.
变量是有名字的参数。
那么子shell确实继承了父shell中带有导出属性的变量或函数。
可参考链接: [执行脚本方式的区别](https://blog.csdn.net/soaringlee_fighting/article/details/78759448)
一般来说,配置交叉编译工具链的时候需要指定编译工具的路径,此时就需要设置环境变量。查看已经存在的环境变量:
shell
[root@localhost ~]# export
declare -x G_BROKEN_FILENAMES="1"
declare -x HISTSIZE="1000"
declare -x HOME="/root"
declare -x hostname="localhost"
declare -x INPUTRC="/etc/inputrc"
declare -x LANG="zh_CN.UTF-8"
declare -x LESSOPEN="|/usr/bin/lesspipe.sh %s"
declare -x logname="root"
declare -x LS_COLORS="no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=01;32:*.cmd=01;32:*.exe=01;32:*.com=01;32:*.btm=01;32:*.bat=01;32:*.sh=01;32:*.csh=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*.tz=01;31:*.rpm=01;31:*.cpio=01;31:*.jpg=01;35:*.gif=01;35:*.bmp=01;35:*.xbm=01;35:*.xpm=01;35:*.png=01;35:*.tif=01;35:"
declare -x mail="/var/spool/mail/root"
declare -x OLDPWD
declare -x PATH="/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"
declare -x pwd="/root"
declare -x SHELL="/bin/bash"
declare -x SHLVL="1"
declare -x SSH_CLIENT="192.168.2.111 2705 22"
declare -x SSH_CONNECTION="192.168.2.111 2705 192.168.2.2 22"
declare -x SSH_TTY="/dev/pts/0"
declare -x TERM="linux"
declare -x USER="root"
[1]: https://www.cnblogs.com/hongzg1982/articles/2101792.html
[2]: https://stackoverflow.com/questions/5564418/exporting-an-array-in-bash-script
[3]: https://unix.stackexchange.com/questions/22796/can-i-export-functions-in-bash
[4]: https://askubuntu.com/questions/26318/environment-variable-vs-shell-variable-whats-the-difference