LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to test if an env variable is set (http://www.linuxquestions.org/questions/linux-newbie-8/how-to-test-if-an-env-variable-is-set-379353/)

King of Men 11-02-2005 03:51 PM

How to test if an env variable is set
 
It's probably rather trivial to do, but I can't quite seem to figure it out. What I want to do is this :

if (ENV_VAR is not set) {
doSomeStuff
setenv ENV_VAR someValue
}

Is there a function in bash that returns true if an env variable is set, and false otherwise? getenv looks like it ought to do the job, but I can't quite figure out how to use it.

acid_kewpie 11-02-2005 04:22 PM

generally you'd say "check if a fixed string is the same as the same string with a given variable on the end" e.g.
Code:

if [ "x" == "x$var" ]; then ... ; else ... ; fi

King of Men 11-02-2005 05:21 PM

But if $var isn't set, the shell complains, thusly :

> if ["x" == "x$SOMEVAR"] echo blah
SOMEVAR: Undefined variable.

acid_kewpie 11-02-2005 05:46 PM

it works fine in bash, what shell are you using?

King of Men 11-02-2005 06:38 PM

tcsh, if my env is to be trusted :

SHELL=/bin/tcsh

acid_kewpie 11-03-2005 05:39 AM

well, i'm afraid i don't know tcsh... would have helped in the first place to be told this... ;)


All times are GMT -5. The time now is 03:35 PM.