LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 02: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, 03:22 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,384

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
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, 04: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, 04:46 PM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,384

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
it works fine in bash, what shell are you using?
 
Old 11-02-2005, 05: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, 04:39 AM   #6
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,384

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
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 09:58 PM
scp error, TERM env. variable not set? jimbo Linux - Networking 2 11-26-2005 01:31 AM
env variable for proxy: syntax FrayAdjacent Linux - Networking 10 07-13-2005 05:44 PM
env variable allelopath Linux - Software 3 04-14-2005 12:07 PM
how do you set an env variable from within a csh script? BrianK Linux - General 3 05-26-2004 02:16 PM


All times are GMT -5. The time now is 05:42 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration