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>PS1 defines the shell's command-line prompt.
!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>
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>
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>
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>
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>
and then, regardless of what directory you are in, you can issue!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 \$ "
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>
Comments - most recent first