LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 04-16-2006, 07:55 PM   #1
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Rep: Reputation: 45
how do I set a system wide enviroment variable? (not just bash)


Hello,

I was wondering if it is possible to set a system wide environment variable that can be used at system initialization, not just when I envoke bash. I added export SRV=192.168.0.101 in my /etc/profile but it is only useful after I log in. I would like to use this variable in a couple of my initialization scripts and in things like my /etc/fstab. I'm pretty new to linux so I don't know all of the neat little tricks yet. Everything I found online was just referring to shell environment variables. Is there a way to set a variable that is global to the linux system? Thanks to anyone taking the time to read this and respond!

...drkstr
 
Old 04-16-2006, 08:15 PM   #2
320mb
Senior Member
 
Registered: Nov 2002
Location: pikes peak
Distribution: Slackware, LFS
Posts: 2,577

Rep: Reputation: 48
TRY:
/etc/rc.d/rc.local
this file is for initializing scripts/commands upon bootup......
 
Old 04-16-2006, 08:39 PM   #3
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Original Poster
Rep: Reputation: 45
I added export TEST=192.168.0.101 to my rc.local but it still did not create a global variable. I belive it only created the variable for /bin/sh since it was still not acsessable from bash or from my /etc/fstab and /etc/exports. I even tried adding it to the beginning of my /etc/rc.M since the rc.local gets called at the end of this file. Still didn't work. Is there a way to make a variable that is not shell specific? thanks for the advice!

...drkstr
 
Old 04-16-2006, 10:03 PM   #4
Lsatenstein
Member
 
Registered: Jul 2005
Location: Montreal Canada
Distribution: Fedora 31and Tumbleweed) Gnome versions
Posts: 311
Blog Entries: 1

Rep: Reputation: 59
Have you looked at the .profile files? There is usually one for bash, and one for the system. The system one is supposed to get executed before your local shell .profile does. (in some cases bash is not your default shell).
 
Old 04-16-2006, 10:46 PM   #5
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Original Poster
Rep: Reputation: 45
I was unable to locate any file by that name. Do I need to create one like I did for ~/.bashrc? If so where should I place it? thanks for the help!

...drkstr
 
Old 04-17-2006, 10:15 AM   #6
Lsatenstein
Member
 
Registered: Jul 2005
Location: Montreal Canada
Distribution: Fedora 31and Tumbleweed) Gnome versions
Posts: 311
Blog Entries: 1

Rep: Reputation: 59
Here is a question for you. Are you trying to pick up TEST in a system environment variable, or are you trying to go to a TEST url. If the latter, why not create an entry in the /etc/host file with TEST and your ip address
 
Old 04-18-2006, 03:54 PM   #7
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Original Poster
Rep: Reputation: 45
Yes, thank you for the advice. I wanted to create a symbolic to all of the computers on my network so I don't have to type out their IP all of the time. I will give the /etc/host file a try and see if it works in the various configuration files I use.

Just out of curiosity though, is it possible to set a non shell specific system variable? If all commands get procesed through a shell, what comes before the shell, and how can I play with it? The reason I switched to linux was to be able to ask questions just like this one. =)

thanks for the help!
...drkstr
 
Old 04-18-2006, 10:07 PM   #8
Lsatenstein
Member
 
Registered: Jul 2005
Location: Montreal Canada
Distribution: Fedora 31and Tumbleweed) Gnome versions
Posts: 311
Blog Entries: 1

Rep: Reputation: 59
Environment variables and shells

You asked if environment variables would be preserved across shells.

Here is a simple thing for you to do.

log to linux. Bash is your default shell.

while at the terminal prompt enter sh

sh is now your default shell

Echo ${HOME} or other environment variable that you know from bash.

do another shell command, ksh (if you have ksh installed)

Try the command again..

exit twice to return to bash

If I gave you the answer, you would not remember. If you try it yourself, you will remember the answer to your question.
 
Old 04-19-2006, 01:40 AM   #9
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Original Poster
Rep: Reputation: 45
I see. environment variables that I set in bash shell get caried over to any other shell that I run from bash. Why is it then, that a variable is undefined in bash when I set it before envoking any shell., (IE defined in rc.local) when you 'export SOMEVAR=bananas, where is it exporting to? (a tmp file?, system memory?) Apparently any shell can access exported variables post log in, why is it different pre login? Again, this thread has turned in to more of a curiosity thing then an actual problem. As it is my nature to always have a million questions, I won't get offended if you don't have the time to keep responding to this tread. But if you have any of the answers, or any other information on how variables work that I might find interesting, please share. Or if you prefer, point me in the right direction to get the answers myself, as you did in your last post. I would prefer the later method myself.

thanks in advance,
...drkstr
 
Old 04-19-2006, 01:51 AM   #10
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
> export test=atest
> bash
> echo $test
atest

The variable $test is visible in the new subshell.
> csh
> echo $test
atest

The variable $test is visible in the new c shell. It inherits it from the environment. The "export" command moves a variable from local storage to the environment.

Quote:
From bashref:
Invoked as an interactive login shell, or with `--login'
........................................................

When Bash is invoked as an interactive login shell, or as a
non-interactive shell with the `--login' option, it first reads and
executes commands from the file `/etc/profile', if that file exists.
After reading that file, it looks for `~/.bash_profile',
`~/.bash_login', and `~/.profile', in that order, and reads and
executes commands from the first one that exists and is readable. The
`--noprofile' option may be used when the shell is started to inhibit
this behavior.
Different shells use different startup scripts. Some others use /etc/profile, but the syntax may not work if you aren't carefull. Csh doesn't use profile, and the syntax for assigning variables is different. However if your system uses bash, and an environment variable is already set, a users new csh will inherit the system environment.
 
Old 04-19-2006, 06:08 AM   #11
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 34
Quote:
Why is it then, that a variable is undefined in bash when I set it before envoking any shell., (IE defined in rc.local)
rc.local is just a script that is run at boot time. It has no relation to your login shell.

Quote:
when you 'export SOMEVAR=bananas, where is it exporting to? (a tmp file?, system memory?)
Every unix process has an environment, a list of key=value strings. Each process inherits its environment from its parent.

The basic login process goes like so. *getty displays the "login:" prompt. You enter your user name and *getty execs /bin/login, passing the user name. login then prompts you for you password, which it verifies. Assuming the password is correct, login then exec's your login shell with a clean environment. login sets some fundamental variables like HOME, USER, LOGNAME, a default PATH etc.

There are various shell configuration files, system wide files in /etc and user files in ~. The filenames obviously differ from shell to shell. E.g., bash reads /etc/profile and ~/.profile for a login shell, then ~/.bashrc (a non-login shell just reads ~/.bashrc). These files then setup the rest of your environment.

So the process sequence is init->getty->login->shell. rc.local is run before any gettys are started, so there is no direct relationship between rc.local and a user shell. Thus, nothing you do in rc.local has any bearing on your shell environment.
 
Old 04-19-2006, 09:09 AM   #12
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Original Poster
Rep: Reputation: 45
Thank you for everyone's input. I think I'm starting to get a much better understanding for how the linux environment works. I hope to some day really understand the concepts behind linux, not just how to use it.

Thanks for the help!
...drkstr
 
  


Reply



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
System Path Enviroment Variable innovations Linux From Scratch 2 10-26-2005 04:27 PM
enviroment variable PATH Moataz Linux - Newbie 2 04-25-2005 08:25 PM
How to set System Enviroment Variables cathodion Linux - General 2 06-22-2004 05:05 PM
Where to set system PATH variable Cybers19 Linux - Newbie 4 05-05-2004 01:46 PM
Enviroment variable set for all ChimpFace9000 Slackware 5 12-09-2002 09:39 AM

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

All times are GMT -5. The time now is 06:47 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