环境变量
用于设置系统运行环境参数的变量--拥有全局特性
查看方法:
- env
- set
- echo+$+环境名称
删除方法:
- unset
常见环境变量:
- PATH:指定搜索命令
- HOME:指定用户主目录
- SHELL:当前shell
setenv函数
#include <stdlib.h>
int setenv(const char *name, const char *value, int overwrite);
int unsetenv(const char *name);
函数说明:setenv()用来改变或增加环境变量的内容。
- name: 环境变量名称
- value:变量内容
- overwrite:若已存在,是否覆盖已存在的环境变量
若环境变量不存在,直接添加
若存在且overwrite == 0 忽略value
export 命令
export [-fn] [name[=word]] ...
export -p
The supplied names are marked for automatic export to the environment of subsequently executed commands. If the -f option is given, the names refer to
functions. If no names are given, or if the -p option is supplied, a list of all names that are exported in this shell is printed. The -n option causes
the export property to be removed from each name. If a variable name is followed by =word, the value of the variable is set to word. export returns an
exit status of 0 unless an invalid option is encountered, one of the names is not a valid shell variable name, or -f is supplied with a name that is not a
function.
export
语法:export [-fnp] [变量名称] = [变量设置值]
- -f 代表[变量名称]中为函数名称。
- -n 删除指定的变量。变量实际上并未删除,只是不会输出到后续指令的执行环境中。
- -p 列出所有的shell赋予程序的环境变量
export设置环境变量是暂时的,只在本次登录中有效
如果想设置永久的变量
- (全体用户)在/etc/profile文件中添加变量
- (单一用户)在用户目录下的.bash_profile文件中增加变量