环境变量 setenv函数 和 export命令

本文详细介绍环境变量的作用、查看与管理方法,包括使用env、set、echo、unset等命令及setenv函数,解释如何通过export命令在shell中设置、列出、删除环境变量,以及如何在系统配置文件中永久设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境变量

		用于设置系统运行环境参数的变量--拥有全局特性

查看方法:

  • 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文件中增加变量
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值