Linux下神奇的环境变量
作者Lou Xiao创建时间2020-04-25 21:46:00更新时间2020-04-25 21:46:00
什么是环境变量(environment variable)
环境变量就是一个变量,在程序运行前已经存在的键值对(key-value),会影响程序的执行行为。用途和命令参数是一样的。
环境变量一般长这样: env-name=env-value
程序如何从用户那里获取信息?
来源 | 说明 |
---|---|
配置文件 | 程序从预定好的文件里面读取运行信息 |
环境变量 | 程序调用系统API,从系统中获取,且只会关心程序需要的环境变量 |
参数 | 程序运行时从参数获取 |
查看、设置环境变量
查看
1.双击鼠标左键复制此行;2.单击复制所有代码。
1
# 查看当前(此时此终端)所有的环境
2
env
3
4
# 查看某个环境变量
5
echo $PATH
设置环境变量
脚本中和shell配置文件常用。 bash/zsh中,一般写到 ~/.bashrc(~/.zshrc)或 ~/.profile
1.双击鼠标左键复制此行;2.单击复制所有代码。
1
# 语法: export env-name=env-value
2
export PATH=/usr/local/bin:$PATH
Tips: 修改完~/.bashrc后,记得 source ~/.bashrc 使它在当前shell中生效
临时设置环境变量,仅在命令行有效
1.双击鼠标左键复制此行;2.单击复制所有代码。
1
# 运行并看看有何不同
2
LANG=en_US.utf-8 date
3
LANG=zh_CN.utf-8 date
常用环境变量
环境变量 | 目标程序(被谁使用) | 说明 |
---|---|---|
HOME | shell(bash/zsh/ksh等等) | 用户家目录 [man 1 bash] |
PWD | shell(bash/zsh/ksh等等) | 当前目录 [man 1 bash] |
LANG | 很多程序 | 当前本地化语言和编码(英文\中文等) [man 1 bash] |
SHELL | bash | 当前shell(The full pathname to the shell is kept in this environment variable) [man 1 bash] |
PATH | shell(bash/zsh/ksh等等) | 执行命令(只输入程序名)时,shell搜索目录 [man 1 bash] |
TMPDIR | 许多程序 | 存放程序产生的临时文件(temporary files) [man 1 bash] |
MANPATH | man | man搜索帮助文档时搜索的目录 [man 1 man] |
LD_LIBRARY_PATH | ld.so | 执行命令时,搜索程序的动态链接库(共享库) [man 8 ld.so] |
LIBRARY_PATH | 编译器(gcc等) | 编译代码时,链接库文件(动态库\静态库)时搜索路径 [man 1 gcc] |
CPATH C_INCLUDE_PATH CPLUS_INCLUDE_PATH OBJC_INCLUDE_PATH | 编译器(gcc等) | 编译代码时, C\C++等头文件的搜索目录(.h.hpp)[man 1 gcc] |
PATH
执行命令(只输入程序名)时,shell查找具体的程序路径,会从$PATH查找;且以第一个为准,目录之间用 : 分割.
安全提示: 不要把 (.) 加入PATH,很危险!
1.双击鼠标左键复制此行;2.单击复制所有代码。
1
export PATH=/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
2
# 加入自己安装的程序路径
3
export PATH="/path/to/exec/dir/:$PATH"
LD_LIBRARY_PATH
如果某个程序还需动态链接库(共享库).so, LD_LIBRARY_PATH 就是来指定动态链接库的搜索目录的,前边的目录具有最好优先级。
参考 man 8 ld.so
1.双击鼠标左键复制此行;2.单击复制所有代码。
1
The programs ld.so and ld-linux.so* find and load the shared libraries needed by a program, prepare the program to run, and then run it.
2
3
Linux binaries require dynamic linking (linking at run time) unless the -static option was given to ld(1) during compilation.
4
5
The program ld.so handles a.out binaries, a format used long ago; ld-linux.so* handles ELF (/lib/ld-linux.so.1 for libc5, /lib/ld-linux.so.2 for glibc2), which everybody has been
6
using for years now. Otherwise both have the same behavior, and use the same support files and programs ldd(1), ldconfig(8) and /etc/ld.so.conf.
7
8
When resolving library dependencies, the dynamic linker first inspects each dependency string to see if it contains a slash (this can occur if a library pathname containing
9
slashes was specified at link time). If a slash is found, then the dependency string is interpreted as a (relative or absolute) pathname, and the library is loaded using that
10
pathname.
11
12
If a library dependency does not contain a slash, then it is searched for in the following order:
13
14
o (ELF only) Using the directories specified in the DT_RPATH dynamic section attribute of the binary if present and DT_RUNPATH attribute does not exist. Use of DT_RPATH is dep‐
15
recated.
16
17
o Using the environment variable LD_LIBRARY_PATH. Except if the executable is a set-user-ID/set-group-ID binary, in which case it is ignored.
18
19
o (ELF only) Using the directories specified in the DT_RUNPATH dynamic section attribute of the binary if present.
20
21
o From the cache file /etc/ld.so.cache, which contains a compiled list of candidate libraries previously found in the augmented library path. If, however, the binary was linked
22
with the -z nodeflib linker option, libraries in the default library paths are skipped. Libraries installed in hardware capability directories (see below) are preferred to
23
other libraries.
24
25
o In the default path /lib, and then /usr/lib. If the binary was linked with the -z nodeflib linker option, this step is skipped.
运行时动态库的搜索路径的先后顺序:
1. 编译代码时指定的动态库搜索路径;
2. 环境变量LD_LIBRARY_PATH指定的动态库搜索路径;
3. 系统配置文件/etc/ld.so.conf中指定的动态库搜索路径;
4. 默认的动态库搜索路径/lib和/usr/lib;
文章目录