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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
11-02-2005, 03:51 PM
|
#1
|
LQ Newbie
Registered: Sep 2005
Distribution: RedHat
Posts: 10
Rep:
|
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.
|
|
|
11-02-2005, 04:22 PM
|
#2
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
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
|
|
|
11-02-2005, 05:21 PM
|
#3
|
LQ Newbie
Registered: Sep 2005
Distribution: RedHat
Posts: 10
Original Poster
Rep:
|
But if $var isn't set, the shell complains, thusly :
> if ["x" == "x$SOMEVAR"] echo blah
SOMEVAR: Undefined variable.
|
|
|
11-02-2005, 05:46 PM
|
#4
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
it works fine in bash, what shell are you using?
|
|
|
11-02-2005, 06:38 PM
|
#5
|
LQ Newbie
Registered: Sep 2005
Distribution: RedHat
Posts: 10
Original Poster
Rep:
|
tcsh, if my env is to be trusted :
SHELL=/bin/tcsh
|
|
|
11-03-2005, 05:39 AM
|
#6
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
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 01:07 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|