在脚本中使用export导出变量值

时间:2023-03-09 14:43:06
在脚本中使用export导出变量值

LD_LIBRARY_PATH环境变量可以用于设置Linux动态库的位置,常见的用法如export LD_LIBRARY_PATH=/home/username/foo:$LD_LIBRARY_PATH[1],但是每次都输入一长串的路径多少有些麻烦,用shell来执行是不错的方式。

首先将export命令保存至export.sh文件,然后将文件修改为可执行权限,最后运行 source export.sh[2]即可。由于子shell对变量的修改无法影响到父shell,因此需要采用source或者.执行,而不是直接运行./export.sh[2]。

[1]Shared libraries with GCC on Linux - Cprogramming.com

[2]shell - How can I make variables "exported" in a bash script stick around? - Unix & Linux Stack Exchange