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 |
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.
|
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 Code:
-bash-2.05b# maxWarnings=10 Code:
-bash-2.05b# 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}." 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}." Code:
-bash-2.05b# echo 'export greetings="Have happy working hours, ${USER}."' >> /etc/profile edit the file Code:
ls -l ~/.bash_profile Code:
ls -l ~/.profile export greetings="Have happy working hours, ${USER}." Method# 2-B Code:
-bash-2.05b# echo 'export greetings="Have happy working hours, ${USER}."' >> ~/.bash_profile Code:
-bash-2.05b# echo 'export greetings="Have happy working hours, ${USER}."' >> ~/.profile Verify your work: Code:
login as: 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 Code:
[demo@host-6-41 demo]$ more .bash_profile Feel free to play with both the files, but first take their backup: Code:
cp .bashrc .bashrc-bk Code:
source .bash_profile |
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. |
All times are GMT -5. The time now is 01:43 AM. |