检查环境变量是否已经设置 [英] check if environment variable is already set

查看:130
本文介绍了检查环境变量是否已经设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个shell脚本,我必须在其中检查是否设置了环境变量,如果未设置,则必须对其进行设置.有什么方法可以检查shell脚本,是否已经设置了环境变量?

I am writing a shell script, where I have to check if environment variable is set, if not set then I have to set it. Is there any way to check in shell script, whether an environment variable is already set or not ?

推荐答案

[ -z "$VARIABLE" ] && VARIABLE="abc"

if env | grep -q ^VARIABLE=
then
  echo env variable is already exported
else
  echo env variable was not exported, but now it is
  export VARIABLE
fi

我想强调一下[ -z $VARIABLE ]是不够的,因为您可以拥有VARIABLE,但是它没有被导出.这意味着它根本不是环境变量.

I want to stress that [ -z $VARIABLE ] is not enough, because you can have VARIABLE but it was not exported. That means that it is not an environment variable at all.

这篇关于检查环境变量是否已经设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆