日期:2014-05-16  浏览次数:20610 次

(转载)设置linux环境变量
In Linux the command line interpreter is known as the shell. Whatever you type at the command line is understood and interpreted by a program and then that program gives you an output after executing your command. This program that understands what you type is called the shell.

Linux comes with quite a few shells such as Bourne Shell, Bourne Again Shell, C Shell, Korn Shell, etc. The default shell for Redhat Linux is ' bash ' which is very popular since being the default, most users start by learning bash. I shall talk about the bash shell only in this article.

Windows users would be familiar with a program called command.com which had to be present for the OS to boot. Command.com is the Windows equivalent of the Linux shell.

Typing the following at the shell

$ echo $SHELL

would give you the name of the current shell you are using. It would most probably be the bash shell in case you are a new user and have been assigned the default shell.

The bash shell is actually a program that is located at /bin/bash and is executed by Linux the moment a user successfully logs in after entering his user-pass. Once this shell starts, it takes over control and accepts all further user commands. The bash shell presents a $ prompt by default (for normal user accounts). You can change this prompt to whatever you like but leaving it at the default is best. Other shells present different prompts. Changing the prompt is explained below.

-

Shell Environment

All the programs that run under Linux are called as processes. Processes run continuously in Linux and you can kill or suspend different processes using various commands. When you start a program a new process is created. This process runs within what is called an environment. This particular environment would be having some characteristics which the program/process may interact with. Every program runs in its own environment. You can set parameters in this environment so that the running program can find desired values when it runs.

Setting a particular parameter is as simple as typing VARIABLE=value . This would set a parameter by the name VARIABLE with the value that you provide.

To see a list of the environment variables that are already set on your machine, type the following

$ env

This would produce a long list. Just go through the list before reading the next part of the article. Linux by default sets many environment variables for you. You can modify the values of most of these variables. A few of the variables that are set are

HOME=/home/david

would set the home directory to /home/david. This is perfect in case your login name is david and you have been given a directory named /home/david . In case you don't want this to be your home directory but some other one you could indicate so by typing the new directory name. The HOME directory is always the directory that you are put in when you login.

There are many advantages of using the HOME variable. You can always reach your home directory by only typing ' cd ' at the prompt, irrespective of which directory you are presently within. This would immediately transfer you to your HOME directory. Besides in case you write scripts that have $HOME present in them to refer to the current HOME directory, these scripts can be used by other users as well since $HOME in their case would refer to their home directories.

-

PATH=/usr:/bin/:usr/local/bin:.

This is a very important environment variable. This sets the path that the shell would be looking at when it has to execute any program. It would search in all the directories that are present in the above line. Remember that entries are separated by a ' : ' . You can add any number of directories to this list. The above 3 directories entered is just an example.
<