Environment variables in the bash shell help you in several ways. Certain built-in variables change the shell in ways that make your life a little easier, and you can define other variables to suit your own purposes. Here are some examples of built-in
shell variables:
!SPAN>· !SPAN>HOME defines the home directory for a user.
!SPAN>· !SPAN>PATH defines a list of directories to search through when looking for a command to execute.
To list the current values of all environment variables, issue the command!SPAN>
env!SPAN>
or list a specific variable with the !SPAN>echo command, prefixing the variable n ame with a dollar sign (the second line shows the result of the !SPAN>echo command):!SPAN>
echo $HOME
/home/hermie!SPAN>
You've already learned how to customize your shell prompt with the PS1 variable. The HOME variable is one you shouldn't mess with, because lots of programs count on it to create or find files in your personal home directory.!SPAN>
Understanding the Path Variable!SPAN>
As in DOS, the shell uses the PATH variable to locate a command. PATH contains!SPAN> a list of dir ectories separated by colons:!SPAN>
echo $PATH
/bin:/usr/bin:/usr/local/bin!SPAN>
When you enter a command, the shell looks in each of the directories specified in PATH to try to find it. If it can't find the command in any of those directories, you'll see a "Command not found" message.!SPAN>
If you decide to put your own programs in a bin directory under your home directory, you'll have to modify the path to include that directory, or the system will never find your programs (unless you happen to be in that directory when you enter the command). Here's how to change your PATH variable so it includes your personal bin directory:!SPAN>
PATH=$PATH:$HOME/bin!SPAN>
So if PATH was set to !SPAN>/bin:/usr/bin:/usr/local/bin beforehand, it would now have the value !SPAN>/bin:/usr/bin:/usr/local/bin:/home/hermie/bin.!SPAN>
Creating Your Own Shell Variables!SPAN>
If you are a programmer, you'll find it handy to create your own shell variables. First issue the command!SPAN>
code=$HOME/projects/src/spew!SPAN>
and then, regardless of what directory you are in, you can issue!SPAN>
cd $code!SPAN>
to pop over quickly to the directory containing the source code for that way-cool spew program you're developing. (The !SPAN>cd command means "change directory.")!SPAN>
A variable assignment like this will work just fine, but its scope (visibility) is limited to the current shell. If you launch a program or enter another shell, that child task will not know about your environment variables unless you export them first. !SPAN>
Unless you know for sure that an environment variable will have meaning only in the current shell, it's a good idea to always use !SPAN>export when creating variables to ensure they will be global in scope--for example,!SPAN>
export PS1="\u \$ "
export code=$HOME/projects/src/spew!SPAN>
And be sure to add these commands to your !SPAN>.profile file so you won't have to retype them eac h time you log in.!SPAN>
Previous Lesson: Stopping a Program
Next Lesson: Help!
Comments (most recent first)
riya_ivin (23 Aug 2010, 08:13)
1)where is environment root in windows and linux.
2)how to set environmental variable in php.
andrew (17 Aug 2010, 15:38)
how do I install a avid pro editing software on my open suse
Phil Reed (11 Aug 2010, 08:23)
Hi, I'm trying to use an environment variable where the name of the
environment variable is contained in a variable..
app=$1 # script is run with 1 parameter which #is the environment variable to use e.g. #x.scr CABLAPP # CABLAPP is the environment variable so I now #want to say cd $app # which needs to expand $app to $CABLAPP #I've tried adding $ to the contents of the variable which i can do, but still doesn't perform the cd command correctly .. how do you extract the contents of a variable to use it as an enviroment variable ?
David Matthews (28 Jul 2010, 15:41)
Remember folk, DOS days with SET IS NOT THE SAME AS env. Save yourself
hours!!
Gusanto (13 Jul 2010, 08:12)
By default, TOMBOY stores notes on ~/.local/share/tomboy/. To alter this
directory I have to override the location of the note directory by setting
the TOMBOY_PATH environment variable. I added this line "export
TOMBOY_PATH=/media/STORAGE/Tomboy_notes" to .bashrc file as I want to store
the notes on /media/STORAGE/Tomboy_notes". Unfortunately, this way did take
effect. Tomboy still looks for the notes on ~/.local/share/tomboy/.
Could you please show me how to do it properly. Thanks.
MartyA (21 Jun 2010, 11:42)
Environmental variables always confuse me especially when installing
software in the ./configure part where it almost always says 'pay attn to
the env var bla bla..
One particular prog I am trying to install is pokerth's SVN version which I have downloaded, but there is in the directions: 1. Type "cd path/to/the/sources". Then do for example "/usr/qt/4/bin/qmake pokerth.pro" to configure the makefile for your system. Pay attention that the QTDIR environment varialbe points to Qt4 not Qt3. For example QTDIR=/usr/qt/4. You can set this variable typing: "export QTDIR=/usr/qt/4" I have tried time and time again to install this and the environmental variable confuses me to no end. In the above where does QTDIR go? I've tried everything I can think to try and still it always tells me unable to create the make file.. I have QT4 installed and all the dependencies. Does the env variable command go in the same line as the line, /usr/qt/4/bin/qmake pokerth.pro and if so where and how? If not then how do I set it? I've been using linux for quite some time and this issue, the env var has ALWAYS gotten my brain tied up in knots where I have to relearn this every time... Help?
Bill Fawcett (09 Jun 2010, 08:15)
Okay, I've altered the PATH cmd in my .bashsrc file to include my personal
bin folder. echo $PATH after rebooting confirms that the change has taken
place. However, when I try to execute my programs from anywhere but the bin
folder I get "Can't open perl script 'pro': no such file or directory."
This is also the case for my BLAST programs (eg: ./makeblastdb). It only
works if I execute it in the bin where its located or point to it
specifically from the cmd line. What am I doing wrong and how do I fix it?
Thanks.
Jay (28 May 2010, 17:11)
I used a guide to set the SWAN_SYNC=0 variable in Windows 7 for use on the
DC Project GPUGRID & it was easy, but I'm a total N00B with Linux. How can
I set this on Ubuntu 10.04 & is it the same on Mint Linux 8?
rohit mahadik (10 May 2010, 21:55)
it is very usefull for good career
thanks!!!!
prathyusha (30 Apr 2010, 00:34)
ya i used BASH: export name=value and it did work
thank you
bash_noob (29 Apr 2010, 11:15)
where is the terminal profile stored?
Bob Rankin (29 Apr 2010, 08:41)
Some versions of Linux use a different command shell. Here are the
commands for csh and bash shells:
CSH: setenv name value BASH: export name=value CSH: unsetenv name BASH: unset name
prathyusha (29 Apr 2010, 01:22)
i have downloaded ATNF Pulsar catalog. it says 'to define an environment
variable called PSRCAT_FILE that points to the public catalog do "setenv
PSRCAT_FILE mydir/psrcat/psrcat.db".
when i do this it says 'setenv command not found' wat do i do now?? i am working with linux and am new to it...... thanks in advance
bash_noob (26 Apr 2010, 01:48)
Ignore my last comment, It's working..
but is there a way to change the GUI terminal prompt too??
bash_noob (26 Apr 2010, 01:37)
I've added echo "I AM HERE" as you suggested but it didn't echo the msg
when I logged back in
I'm running lucid-rc on virtual box although i dont think that should be the problem, I'll try it with an older distro....thanks for the great site though i'm learning a lot ...:-)
Bob Rankin (25 Apr 2010, 14:30)
Can I assume you've logged out and logged back in, so the .profile was
executed? I'll suggest adding an 'echo "I AM HERE"' command to the
.profile so you can be sure it's running at all.
bash_noob (24 Apr 2010, 22:09)
I've added 'export PS1="\u \$ "' at the end of .profile present in
(home/user) but it doesn't seem to work, what am I missing here?
Bob Rankin (13 Apr 2010, 08:04)
Shell environment varaibles are limited in scope to that particular shell.
arkp (12 Apr 2010, 14:00)
I create a var in a (bash) shell with export but don't see that var in
other shells. I tried running 'bash' in the other shells but that didn't
help.
How can I see vars I create in a shell across other shells?
Julio Fernandez (09 Apr 2010, 02:44)
Reply to satheesh: Edit your /root/.bash_profile , and then re-login to
make changes take effect.
Regards
satheesh (08 Apr 2010, 19:40)
Which of these files do you edit to make a permanent change to an
environment variable for the root user?
Bob Rankin (08 Apr 2010, 14:58)
First off, I recommend that you make a backup copy of any config file
before changing it. For example:
cp /etc/php.ini /etc/php.ini0 Next, what changes are you trying to make?
Oscar Vargas Torres (07 Apr 2010, 20:20)
Hi! I've always used Windows. Now I'm using Ubuntu 9.10 and I've modified
some files in the /etc/ subdirectory.
Sadly, each time I've modified the file, I've had to install EVERYTHING again: UBUNTU, and all the applications I'm using. I know how to make temporal changes to environment variables, but I can't find the way to make system-wide changes. I NEED HELP!!!!!!!!!!!!!!!
Andy Aunan (07 Apr 2010, 12:11)
in a directory like /home/abcdef01 a user runs a commad called
VMmenu, but he need to run it ./VMmenu. the question how eliminate the
(./) , i was think it was a path issue, but now i am not sure
Bob Rankin (01 Mar 2010, 06:37)
The backslashes are escape characters. They tell the Bash shell to
substitute those items into the command prompt.
sri (27 Feb 2010, 02:05)
thanx
Dennis (26 Feb 2010, 07:51)
export PS1="\u \$ "
export code=$HOME/projects/src/spew could you inform me please what is with the backslash from first line in relation with slash from the second one.? |
|
|
![]() |
|
| <Send This Link to a Friend> <Bookmark This Page> | ||