LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-10-2011, 09:31 AM   #1
toma20082010
LQ Newbie
 
Registered: Feb 2011
Posts: 22

Rep: Reputation: 0
Thumbs up confusion about permanent env variables


hi there,

when we set an permanent environment variable we set it like this VARIABLE = WHATEVER WE WANT,
but where exactly we do that?
because i was searching for it and it was confusing.
some say in .bash_profile
or .profile
or .bashrc
and whether in /home directory or /etc directory.
im using bash shell.

thanks
 
Old 03-10-2011, 12:18 PM   #2
corp769
Guru
 
Registered: Apr 2005
Posts: 5,807

Rep: Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996
Honestly that is up to you. Are you trying to set variables before you start a bash session? You could use either .bashrc or .bash_profile; The files in your home directory would be set for only your user, and the config files in /etc are for global settings.
 
Old 03-10-2011, 01:01 PM   #3
devUnix
Member
 
Registered: Oct 2010
Location: Bengaluru, India
Distribution: RHEL 5.1 on My PC, & SunOS / Sun Solaris, RHEL, SuSe, Debian, FreeBSD and other Linux flavors @ Work
Posts: 557

Rep: Reputation: 46
Bash Variables - Temporary and Persistent

Try to grasp as much of what follows here as possible:

(Temporary) Bash Variables:

varName=value
Example# 1:

Code:
-bash-2.05b# logFile=/tmp/myLog
-bash-2.05b# echo $logFile
/tmp/myLog
Example# 2:

Code:
-bash-2.05b# maxWarnings=10
-bash-2.05b# echo $maxWarnings
10
Example# 3:

Code:
-bash-2.05b# developerName="Mr. Dev"
-bash-2.05b# echo $developerName
Mr. Dev

Those variables will not be available when your script terminates or login session is closed.


Persistent Bash Variables:

export variableName=value

Example# 4:

Code:
-bash-2.05b# export greetings="Have happy working hours, ${USER}."
-bash-2.05b# echo $greetings
Have happy working hours, root.
-bash-2.05b# cat > persVar.sh
#!/bin/bash
echo $greetings
-bash-2.05b# chmod +x persVar.sh
-bash-2.05b# persVar.sh
Have happy working hours, root.
Now decide on:

You want to use a persistent variable whenever

[1] any user logins (in the present and in the future as well)
[2] or only you login

Method# 1-A

edit /etc/profile
Insert the following line at an appropriate place or at the end of the file:

Code:
export greetings="Have happy working hours, ${USER}."
Method# 1-B

Code:
-bash-2.05b# echo 'export greetings="Have happy working hours, ${USER}."' >> /etc/profile
-bash-2.05b# tail -n 1 /etc/profile
export greetings="Have happy working hours, ${USER}."
Method# 2-A

edit the file

Code:
ls -l ~/.bash_profile
OR

Code:
ls -l ~/.profile
Insert the following line at an appropriate place or at the end of the file:

export greetings="Have happy working hours, ${USER}."


Method# 2-B

Code:
-bash-2.05b# echo 'export greetings="Have happy working hours, ${USER}."' >>  ~/.bash_profile
OR

Code:
-bash-2.05b# echo 'export greetings="Have happy working hours, ${USER}."' >>  ~/.profile

Verify your work:

Code:
login as: demo
demo@linuxzoo.net's password:
[demo@host-6-41 demo]$ echo $greetings
Have happy working hours, demo.
[demo@host-6-41 demo]$

A note on .bash_profile or .profile versus .bashrc

When you login, your .bash_profile or .profile file is executed which in turn first checks whether .bashrc exists or not. If it does, then it is first called:

Code:
[demo@host-6-41 demo]$ more .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi
That means that you can write your "permanent bash variables" in .bashrc after (preferably) "# User specific environment and startup programs" or in your .bash_profile (or .profile) file as shown earlier.

Code:
[demo@host-6-41 demo]$ more .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

Feel free to play with both the files, but first take their backup:

Code:
cp .bashrc .bashrc-bk
cp .profile .profile-bk
Note: If you edit .profile or .bash_profile or .bashrc then you can bring the variables to life without having to relogin:

Code:
source .bash_profile
Cheers!

Last edited by devUnix; 03-10-2011 at 01:04 PM.
 
Old 03-10-2011, 01:03 PM   #4
business_kid
Guru
 
Registered: Jan 2006
Location: Ireland
Distribution: Slackware & Android
Posts: 6,495

Rep: Reputation: 568Reputation: 568Reputation: 568Reputation: 568Reputation: 568Reputation: 568
All over the place is where we set things. Some places

/etc/profile.d/*
/etc/bashrc
~/.bash_profile
~/.bashrc
files in /etc/rc.d/
/etc/environment (If it exists)
Then there's the glibc locale stuff, /etc/X11/*

But never mind that. You can fiddle the stuff yourself. The syntax is

STRANGEVAR="mad value"
export $STRANGEVAR

The favourite one is $PATH; everyone fiddles that. and with reason, because you can limit the directories that something has access to. lusers generally don't have the ~/sbin directories in their path.
 
  


Reply

Tags
bash, environment variable, permanent


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
changing env variables aceone Red Hat 7 06-25-2012 06:13 AM
Getting env variables in X wild_oscar Linux - General 3 04-29-2007 09:49 AM
env variables AbrahamJose Solaris / OpenSolaris 4 04-18-2006 12:31 PM
env variables ekdya Debian 3 03-14-2006 01:33 PM
Environmental variables... permanent fabianrios Fedora 2 10-20-2005 05:06 PM


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