-   Linux - Newbie (
-   -   How to test if an env variable is set (

King of Men 11-02-2005 02: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) {
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 03: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.

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

King of Men 11-02-2005 04: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 04:46 PM

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

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

tcsh, if my env is to be trusted :


acid_kewpie 11-03-2005 04: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 04:56 AM.