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.
Note : The last entry in the PATH command is a ' . ' (period). This is an important addition that you could make in case it is not present on your system. The period indicates the current directory in Linux. That means whenever you type a command, Linux would search for that program in all the directories that are in its PATH. Since there is a period in the PATH, Linux would also look in the current directory for program by the name (the directory from where you execute a command). Thus whenever you execute a program which is present in the current directory (maybe some scripts you have written on your own) you don't have to type a ' ./programname ' . You can only type ' programname ' since the current directory is already in your PATH.
Remember that the PATH variable is a very important variable. In case you want to add some particular directory to your PATH variable and in case you try typing the following
PATH =/newdirectory
This would replace the current PATH value with the new value only. What you would want is to append the new directory to the existing PATH value. For that to happen you should type
PATH=$PATH:/newdirectory
This would add the new directory to the existing PATH value. Always a $VARIABLE is substituted with the current value of the variable.
-
PS1=boss
PS1 is the shell prompt. It defines what you want your shell prompt to look like. By default it looks like a ' $ ' in bash shell. The above case would replace the default ' $ ' with a new 'boss' . Hence an ls command would look something like
boss> ls
All your commands would now be typed at a ' boss ' prompt instead of a ' $ ' prompt.
-
SHELL=/bin/bash
This tells where the program that represents your shell is to be found. In case you typed /bin/ksh in the above, then your bash shell would be replaced with the ksh shell (korn shell). So in case you are not happy with the bash shell, you could replace the bash with some other shell.
-
LOGNAME=david
The LOGNAME is automatically set for you as the same as your login name. This variable is used in case you want to use your own login name in any script. This is the simplest way of getting your login name from within a script. Thus in case you use $LOGNAME in any script the script would work for all users since the LOGNAME always holds the name of the current user.
A good use is in case you have been given a temporary directory to work with and to make temporary files then you would want to delete the files that you created. You could use a command with the $LOGNAME in it to locate files that were created by you and then you could pass the result of this command to a rm command. This would be a neat way to delete all the files at one go, rather than find them one at a time.
Note : Finding files based on a particular criteria and then passing those files to another program is explained in Article No. 21
-
There are more environment variables then the ones that are mentioned here. But most users would find the ones given here to be useful.
Important : To make the above changes permanent (that is it should work every time you login) make the changes to the .profile file that exists in your HOME directory. Simply type the required commands one line for each. And there you go. It will be available every time you login. You could check the variables using 'env' command.
本文只是为了个人学习和记录之用,原文转自tipsforlinux
[url]http://www.codecoffee.com/tipsforlinux/articles/030.html[/url]
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.
Note : The last entry in the PATH command is a ' . ' (period). This is an important addition that you could make in case it is not present on your system. The period indicates the current directory in Linux. That means whenever you type a command, Linux would search for that program in all the directories that are in its PATH. Since there is a period in the PATH, Linux would also look in the current directory for program by the name (the directory from where you execute a command). Thus whenever you execute a program which is present in the current directory (maybe some scripts you have written on your own) you don't have to type a ' ./programname ' . You can only type ' programname ' since the current directory is already in your PATH.
Remember that the PATH variable is a very important variable. In case you want to add some particular directory to your PATH variable and in case you try typing the following
PATH =/newdirectory
This would replace the current PATH value with the new value only. What you would want is to append the new directory to the existing PATH value. For that to happen you should type
PATH=$PATH:/newdirectory
This would add the new directory to the existing PATH value. Always a $VARIABLE is substituted with the current value of the variable.
-
PS1=boss
PS1 is the shell prompt. It defines what you want your shell prompt to look like. By default it looks like a ' $ ' in bash shell. The above case would replace the default ' $ ' with a new 'boss' . Hence an ls command would look something like
boss> ls
All your commands would now be typed at a ' boss ' prompt instead of a ' $ ' prompt.
-
SHELL=/bin/bash
This tells where the program that represents your shell is to be found. In case you typed /bin/ksh in the above, then your bash shell would be replaced with the ksh shell (korn shell). So in case you are not happy with the bash shell, you could replace the bash with some other shell.
-
LOGNAME=david
The LOGNAME is automatically set for you as the same as your login name. This variable is used in case you want to use your own login name in any script. This is the simplest way of getting your login name from within a script. Thus in case you use $LOGNAME in any script the script would work for all users since the LOGNAME always holds the name of the current user.
A good use is in case you have been given a temporary directory to work with and to make temporary files then you would want to delete the files that you created. You could use a command with the $LOGNAME in it to locate files that were created by you and then you could pass the result of this command to a rm command. This would be a neat way to delete all the files at one go, rather than find them one at a time.
Note : Finding files based on a particular criteria and then passing those files to another program is explained in Article No. 21
-
There are more environment variables then the ones that are mentioned here. But most users would find the ones given here to be useful.
Important : To make the above changes permanent (that is it should work every time you login) make the changes to the .profile file that exists in your HOME directory. Simply type the required commands one line for each. And there you go. It will be available every time you login. You could check the variables using 'env' command.
本文只是为了个人学习和记录之用,原文转自tipsforlinux
[url]http://www.codecoffee.com/tipsforlinux/articles/030.html[/url]