LINUX CLASSES - THE BASICS
Linux Environment Variables
How Do I Set and Use Linux Environment Variables? Environment Variables
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
· PS1 defines the shell's command-line prompt.
· HOME defines the home directory for a user.
· 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
or list a specific variable with the echo command, prefixing the variable n
ame with a dollar sign (the second
line shows the result of the echo command):
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.
Understanding the Path Variable
As in DOS, the shell uses the PATH variable to locate a command. PATH contains a list of dir
ectories separated by colons:
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.
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:
So if PATH was set to /bin:/usr/bin:/usr/local/bin beforehand, it would now
have the value
Creating Your Own Shell Variables
If you are a programmer, you'll find it handy to create your own shell variables. First issue the command
and then, regardless of what directory you are in, you can issue
to pop over quickly to the directory containing the source code for that way-cool spew program you're
developing. (The cd command means "change directory.")
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.
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 export when creating variables to ensure they will be global in scope--for
export PS1="\u \$ "
And be sure to add these commands to your .profile file so you won't have to retype them eac
h time you log in.
Previous Lesson: Stopping a Program
Next Lesson: Help!
[ RETURN TO INDEX ]
Comments - most recent first
(Please feel free to answer questions posted by others!)
I welcome your comments. However... I am puzzled by many people
who say "Please send me the Linux tutorial." This website *is* your Linux Tutorial! Read everything here, learn
all you can, ask questions if you like. But don't ask me to send what you already have. :-)
NO SPAM! If you post garbage, it will be deleted, and you will be banned.