LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-02-2005, 03:51 PM   #1
King of Men
LQ Newbie
 
Registered: Sep 2005
Distribution: RedHat
Posts: 10

Rep: Reputation: 0
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.
 
Old 11-02-2005, 04:22 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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
 
Old 11-02-2005, 05:21 PM   #3
King of Men
LQ Newbie
 
Registered: Sep 2005
Distribution: RedHat
Posts: 10

Original Poster
Rep: Reputation: 0
But if $var isn't set, the shell complains, thusly :

> if ["x" == "x$SOMEVAR"] echo blah
SOMEVAR: Undefined variable.
 
Old 11-02-2005, 05:46 PM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
it works fine in bash, what shell are you using?
 
Old 11-02-2005, 06:38 PM   #5
King of Men
LQ Newbie
 
Registered: Sep 2005
Distribution: RedHat
Posts: 10

Original Poster
Rep: Reputation: 0
tcsh, if my env is to be trusted :

SHELL=/bin/tcsh
 
Old 11-03-2005, 05:39 AM   #6
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
well, i'm afraid i don't know tcsh... would have helped in the first place to be told this...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
socks proxy - env variable andrewlkho Linux - Networking 1 06-23-2013 10:58 PM
scp error, TERM env. variable not set? jimbo Linux - Networking 2 11-26-2005 02:31 AM
env variable for proxy: syntax FrayAdjacent Linux - Networking 10 07-13-2005 06:44 PM
env variable allelopath Linux - Software 3 04-14-2005 01:07 PM
how do you set an env variable from within a csh script? BrianK Linux - General 3 05-26-2004 03:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration