The content of this article is derived from the Basics section of MUO, and its original English version can be obtained from here.
http://www.mandrakeuser.org/docs/. Chinese version of CMUO from Wu Xiaoguang
http://dummy.linux.net.cn/~xgwu/cmuo/. MUO is Mandrake Linux (
http://www.mandrakelinux.com/) Provides the user's entry manual, its content is practical and real-time update, and it is very suitable for beginners to do new. Unlike common various Linux tutorials, MUO introduces Linux beginners are learning Linux methods rather than a description of a system, which is especially important for all kinds of LINUX distribution. This article has made a corresponding deletion process on the relevant chapters when compiling, removes some of the contents of Mandrake Linux.
Use shell
The basic shell commands and mechanisms will be described below and explained below.
First: Super Tools / Terminals, XTerms and Shells
Super tool
You may have heard such an argument: The Mommand Line has long been outdated, and that is mysterious, and so on. Some people even think that there should be no such commands in the operating system.
The truth is on, you can use Linux without knowing any shell. After you start the system, you can go directly to X Window directly, and finally shut down under x window.
I firmly believe that I don't understand the shell with Linux, just like a first gear. Of course, it initially looks directly and simple, in most cases. But the speed is slow, and you can't really experience the fun of driving.
Yes, the command line is very interesting. Just like the building blocks collected by a lot, you can do many unexpected innovations, some extremely complex work, just a few commands can be resolved. This is because, in UNIX, the shell is not a simple command interpreter (typical with DOS in Windows), but a full-featured programming environment.
This doesn't mean that shell is very easy to learn. You know, good things, this is still speaking. ;-) But please believe me, this is definitely worth it. You can be touted for Unix Wizard (Wizard) by a big gangster outside in a short period of time. * Grin *
To illustrate the shell, some background knowledge is required.
Terminals, xterms with shells
Take back to the age of Unix, there is no current "personal computer" that is now popular. A machine that is called a computer or a homogenous tape with MagNetic Memory (using the term 'core' to represent system Memory). The PDP-11 launched by DEC (now Compaq) is small (known as mini) and the price of the price, causing a huge response in the university, and many schools have only bought a computer (PDP-11) Good quality, only 10,000 $).
The operating system of these machines is written by assembly languages, so that running is high, but unportable. Every computer company is equipped with a unique operating system for their own machines and then sell.
This clumsy approach is quickly realized, so I started to build an operating system that can run on different brands. In 1969, Ken Thompson began to write the first line of code that UNIX. (Thompson once participated in a project: Multics, Unix is a joke related to this).) In fact, Dennis Ritchie designed a new programming language for this new operating system, after things really start. Although UNIX has no efficiency as the original operating system, there are three prominent advantages: can be transferred to other machines, which greatly simplifies programming, and these are free. Soon, the University of the United States is busy starting to install UNIX for the machine.
Terminals
UNIX is an operating system that can be run on many machines, but how people use these machines? They are connected to these machines through a dumb terminal, which is connected to the Central Computer with a keyboard, a display, and a machine that consists of sufficient Electronics (electronic components). On these terminals, the user can knock the character (TELETYPY), which is the name of the string 'TTY' represents the terminal device file, and the name of the 'getty' command.
You may ask, where are you now. The manufacturers of these terminals cannot reach a final standard, which leads to the terminals of each brand have their respective keyboard layout, and their respective methods of displaying characters on the screen, what characters, control code, etc. are sent or received.
In order to avoid these confusion, a (Capability) file containing all different terminal features is created, which is 'Termcap'. Open '/ etc / termcap' with a tool, don't be scared ;-).
Most of the Linux terminal uses 'VT100' or 'Linux' as a terminal type.
XTERMS
In the early 1980s, a UNIX graphic subsystem was produced - The x window system. In the early 1990s, in order to better achieve applications on Intel-based Unix system (such as FreeBSD, NetBSD, Linux), a system branch is generated.
A great advantage in X Window is to run multiple virtual terminals. Even in X Window, there is such an application - 'xterm'. You will find 'xterm' and 'Virtual Terminal' in many cases. Some places say 'open an xterm', in fact, you don't have to use the 'xterm' program, other terminal simulator, such as RXVT, Konsole, ATERM, ETERM, WTERM, etc., as effective.
The terminal simulator (also known as a virtual terminal) is connected to the system through a Pseudo TTY device - PTY and uses its own display standard - xterm. This causes different terminal simulators to have a small difference in some buttons or programs, depending on how much the simulator has complied with the display criteria of 'xterm'.
Shells
In order to run the program in the terminal, you need a shell. The shell is part of the operating system used to deal with the user and can be used to coordinate each command.
The first real UNIX shell - 'sh' is also called 'bourne shell', born in 1975, the author is Steve Bourne. Soon, other shells appear, such as the original 'Bourne Shell' '' KSH ',' ZSH ', the latter is often used as a standard shell in the exclusive Unixes system; there are also some shells derived from the C language, such as' csh 'Or' tcsh '. In Linux, the label is 'Bash', the GNU Bourne-Again Shell (a bit jokeful ...). This shell feature is very powerful (even if someone feels too large), the compressed man page has 50 kB.
Shell starts
First of all, there is a little novel: In the usual application, it is recommended that you do not run the shell with the 'root' account. If you are still a newbie, this is especially paying attention to. As a regular user, no matter what you intend to, it is unable to destroy the system; but if it is 'root', it will be different. As long as you knock a few letters, it may cause catastrophic consequences.
When you log in to your system or open an XTERM window, you first see the prompt (Prompt). The standard prompt of the Red Hat Linux includes your username, the host name (if not set, is 'localhost'), the current directory (Working Directory), prompt symbol:
[Tom @ Belbo Tom] $
I am using the user name 'Tom' host. The host is currently in my home directory - '/ home / Tom'. 'root' prompt:
[root @ belbo root] #
In addition to different user names, the prompt symbol is turned by '$'. According to the tradition of the Bourne Shell, the ordinary user's prompt ends with '$', and the super user uses '#'.
Every part of the prompt can be customized, and you will have a deeper understanding later.
To run the command, you just knock in the command after the prompt and then press
[Tom @ Belbo Tom] $ WHOAMI
Tom
[Tom @ Belbo Tom] $
In the way, when you knock Enter, where the cursor is not tight, because the shell will always read it all.
Basic commands are: 'ls' (List Directory, listing content),' cp '(copy, copy),' mv '(Move / Rename, Mobile / Rename),' CHANGE DIRECTORY (Change Directory) Behind these commands can keep up with a group of options, which is a detailed introduction (MAN LS, MAN MV, etc.). Here you will go to the Shell territory, there are a short description of terms (TERMINOLOGY). Commands may take some options (Options): arguments:
MV -I File Dir
Where '-i' is an option of command 'mv', and 'file' and 'DIR' are parameters. All options have a detailed introduction in the Man Page of this command (in this case with Man MV), and parameters are provided. Options determine how the command works, and the parameters are used to determine the target of the command.
So far, it is a little bit like many people who are disgusted, but with the introduction below, you will have a new experience.
Second: Automatic Extraordinary / Command Line History / Edit Command Line / Available Shell Shortcuts
UNIX (and successor Linux) is born under the command line, so the command line in UNIX has many very useful features. In this article, we will make some understanding.
Automatic complement
How to use 'CD' (changed to directory, Change Directory) Jump from '/ usr / src / redhat /' from your current home directory?
CD / u
This is called the 'command line automatic complement' (Automatic Command Line Completion), which is indispensable in usual applications. Let's take a closer look at this example:
CD / u
Expanding CD / USR /, very simple. Below
CD / u
Expansion is CD / USR / SRC /. If you only knock the CD / U
Therefore, the
In addition, it is particularly convenient when you encounter a long text name. Suppose you want to install an RPM package called 'BoomshakAlakWhizBang-4.6.4.5-i586.rpm', you entered rpm -i boom
It will be extended into CD / USR / SRC / Linux and wait for continuation. There are two matching directories in '/ usr / src': '/ usr / src / linux - [...]', '/ usr / src / linux'. How do I tell Shell you want later? Just follow one slash (/, slash), you can choose the following.
If you are not sure is '/ usr / src / linux / documentation' or '/ usr / src / linux / documentation'. You know, Linux is case sensitive. If you have read the front part carefully, you can use it:
CD / u
Extension into '/ usr / src / linux / drivers /', so it should be 'Documentation' (uppercase 'd').
This complement is also valid for the command:
[Tom @ Belbo Tom] $ GRE
GRecord Grefer Grep
[TOM @ Belbo Tom] $ GRE
Here shell will list all known commands at the beginning of the string 'gre'.
History of command line
By pressing the uplier key, you can traverse the commands recently entered under this console. Use the down direction key to traverse forward. If you use the Shift key, you can also traverse the output in this console. You can also edit old commands and then run.
After pressing
(Reverse-I-Search) `':. Typing' I 'may become:
(Reverse-I-Search) `i ': isdnctrl Hangup ippp0
If you press the
Edit command line
By cursor and function keys (HOME, END equation), you can browse and edit the command line, if you need, you can also complete the general editor using the keyboard shortcut:
L
l
l
l
l
L
l
l
l : Repeat the last parameters of the previous command.
For example: You use a directory with the command mkdir peter / pan / documents / tinkerbell, now you enter the directory with the command 'CD', you can use CD! $, Shell will add the previous command 'mkdir' parameters to Now 'cd' behind.
When you know more about Linux, you will see that these shortcuts are entered under other applications, such as in the input box in the browser.
Available shell shortcuts
Red Hat Linux has many shortcuts, some of which are Bash original, and some are set for you (after you will see).
Since the Home directory is the activity center of each user, many UNIX has special shortcuts.
'~' Is the shorthand form of your home directory. We assume that you are in other directories, want to copy a file called 'Sometext' to the 'DOCS' subdirectory under your home directory. In addition to input:
CP Sylext / Home / MyuserName / DOCS
You can also use yourself:
CP SomeText ~ / DOCS
In theory, this can also be applied to the command 'cd'. No matter where the current path is, CD ~ will return to your home directory. In fact, it can be simplified, as long as the CD is typed, you can return home directory.
Red Hat Linux provides you with some pre-set shortcuts (called 'alias', aliases), such as:
ll: The 'ls -l -k' will be executed (listed in a long format, including some file properties, and display file size in KB instead of byte)
lls: will execute 'ls -f --color = auto' (list the contents of the directory, plus file type ID, and use color)
Now, you should have a further understanding of Shell and some shortcuts. Let's take a look at what except for some simple commands.
The third article: The order of the command / command is replaced by the task scheduling / command
Ordered arrangement
Now you will see some commonly used commands. You may want to give all the commands in a row, then you can transfer your attention to elsewhere. No problem, shell allows you to put a special arrangement between different commands (Queuing Characters). This will introduce two of the most commonly used.
Please note that in order to look clearer, I added a space on both sides of these characters. In practical applications, you don't have to do this, 'ls -a; du-hs' and 'ls -a; du-hs' effect is the same.
Command1; Command2
Execute Command1 first, regardless of whether Command1 is wrong, then execute Command2.
E.g:
Ls -a; du -hs
Will first list all the contents in the directory on the screen, then list all directories and their subdirectories.
Command1 && Command2
Only when Command1 is running correctly after the COMMAND1 is running correctly. E.g:
Ls -a bogusdir && du-hs
The LS: BogusDir: No Such File or Directory, and 'du' is not running at all (this is because you don't have the 'bogusdir' directory). If you replace the symbol to ';', 'du' will be executed.
In order to further illustrate ';' and '&&', the use of the general command arrangements, the following is a classic example: the compilation and installation of the Linux kernel.
To compile, install Linux, you need to perform a string command: 'make dep', 'make clean', 'make bzimage', 'make modules', 'make modules_install', and 'make install'. If you want to wait after a command is completed, enter the next one, wait, then enter, ..., it is too much trouble. On the other hand, each command can only be implemented only when the previous command is executed correctly. If you use ';' to arrange the command, even if there is a command to perform failed, then run as usual, and finally, you may get a problematic kernel image (image) in the '/ boot' directory. And use '&&':
Make DEP && Make Clean && Make Bzimage && make MODULES & Make Modules_install && made install
You can compile the kernel and its modules without a midway, and complete the installation.
Task scheduling of the command
When you run a command in the terminal or when a program is turned on, the terminal will wait until the command or the program is running, it can be used again. In UNIX, we call such commands or programs running in the foreground. If you want to run another command under the terminal, you need to open a new terminal.
But there is a more elegant approach called task scheduling or backgrounding. When you use the task's scheduling or place the command in the background, the terminal is liberated, so that the terminal can immediately accept new inputs. To achieve this, you only need to add a &:
GQVIEW &
Tell the shell to put the picture viewer 'gqview' into the background (ie, as Job is running).
Command Jobs will tell you that in this terminal window, which commands and programs are running:
Jobs
[1] Running GQVIEW &
This is important when you want to close the terminal window, because the closing terminal will cause all tasks running in it, will be aborted, in this case, if you turn off the terminal, the GQVIEW program that is turned on by this terminal will also is closed.
But how do you put a program running in the front desk in the background? no problem:
GQVIEW
[2] Stopped GQVIEW
BG
[2] GQVIEW &
The combination of
Some graphics probes are likely to be in the test period (Beta), although execution in the background, some information will be output in the terminal. If you are dissatisfied with this, you can use the following command:
Command &> / Dev / Null &> / Dev / Null &> / DEV / NULL &>
This not only sent the program to the background, but also output it to the '/ dev / null' file. '/ dev / null' is the "shredder" of the system, all information sent there will disappear.
Replacement of the command
Command Substitution is a very practical feature. We assume that you want to see the 'Readme.Mouse' file in the XFree86 document, but you don't know the location of this file. But you are a user, I have heard of the 'locate' command, and the 'slocate' package is installed, you can use:
Locate Readme.Mouse
Discover that file in '/ usr / x11r6 / lib / x11 / doc'. Now you can use 'less' in the terminal or enter that directory in the file manager and then read the file. The command replacement can bring you some convenience:
Less $ (Locate Readme.Mouse)
complete in one step. The output "= /usr/x11r6/lib/x11/doc/readme.mouse) is the parameter of 'Less', and then the file content is displayed.
The syntax of this mechanism is:
Command1 $ (Command2)
In addition to '$ ()', you can also use the backquote:
Command1 `command2`
Although it can reduce input, but readability is poor, and it is easy to confuse the general single quotes without replacement. I appreciate the former approach, but this finally pays attention to you.
There is another example here. We assume that you intend to end a program called 'Rob'. You first use the command 'pidof' to find the corresponding process number (Process ID), then use this PID as a parameter, run the 'kill' command, so you can end the 'Rob' program. In addition to:
Pidof rob
567
Kill 567
You can also try:
Kill` Pidof Rob`
How is it, the efficiency is improved?
In the next article, I will then introduce two other practical mechanisms of the shell: the file name match, output redirection.
Article 4: File Name Matching / Output Redirection
File name match
File name match makes you don't have to write a name one by one, you can specify multiple files. You will use some special characters called wildcards.
Suppose you want to use the 'rm' command to delete all files ending with string '.bak'. In addition to keeping all file names after 'RM', you can use wildcard '*':
Rm * .bak
'*' Can match one or more characters. In this example, you tell the shell to extend the parameters of the command 'rm' to "Files ending with '* .bak'", the shell tells the extended parameters to the 'RM' command. You will see that the shell will read and explain the command line before the command is executed. It is because of this, you can use wildcards to use the parameters of the shell command.
Let us learn more about wildcard '*'. Assume that you have a directory, which contain file '124.bak', '346.bak', and '583.bak'. You want to keep only file '583.bak', you can use:
Rm * 4 * .bak
The shell will extend '* 4 * .bak' extends "all the string containing '4' and ending with '.bak'."
Note that RM 4 * .bak can't work because this is a file that starts with '4'. Since there is no such file in the directory, the shell extends this mode to an empty string, so 'RM' will return an error message:
RM: Cannot Remove `4 * .bak ': No Such File or Directory
If you want to keep file '345.bak', delete '124.bak' and '583.bak'. This looks a little difficult because the name of the deleted file is different from the suffix. But fortunately, you can use the file that does not contain the file:
Rm * [! 6] .bak
This will be read as: In addition to files ending with '6.bak', deleting other files ending with '.bak'. You must put the anti-NEGATION SIGN to the inverter (here 6), otherwise, the shell explains the starting mark to the beginning of history replacement (The Beginning of A History Substitution) . Take the inverted number in all matching modes introduced in this article.
Please note: Tongcaver '*' is used with the reverse number, it is easy to generate problems. Guess
Rm * [! 6] *. Bak
What did you say? This command will delete all files, even including files containing '6'. If you put the wildcard '*' in front and back, it is actually taking the reaction mark, because the shell explains it as "files that do not contain the character in all names". In our example, only file '666.bak' does not comply with this mode.
The second wildcard is Question Mark: '?'. When matching, a question mark can only represent a character. To demonstrate their use, we add two new files in the hypothetical assumption: '311.bak ~' and 'some.text'. Now list all files with four characters after the order:
Ls *. ????
Questionnaire Wildcards can effectively avoid the 'Trevered Tracks (Negation Trap) mentioned above:
RM * [! 4]? *
The expansion is "all files in the previous count of second characters" 4 '", that is, only the file' 346.bak '.
You may ask, have there any other matching method? So far, you only see how the unique characters are matched in the specified location. But actually you can also do this:
LS [13] *
All files starting with characters '1' or '3'; in our example, file '124.bak', '311.bak ~' and '346.bak' match. Note that you must enclose the match in the middle parentheses, otherwise the mode only matches the file that starts with the string '13'. Next, you will be happy to see the range of matching:
Ls * [3-8]? *
Put the second character before all point numbers falls in the '3' to '8' range of files. In our example, the matching file is '346.bak' and '583.bak'.
Quote Special characters for Shell
However, the above mechanisms have a disadvantage: shell always tries to expand before command execution. Sometimes it will become very tricky:
l The file name contains special characters. Suppose you have a file called '! 56.bak' in that directory. The following attempts to make a mode match:
RM! *
RM
RM: Too Few Arguments
Shell explains '! *' to a history of history (join all parameters of the previous command), not a matching method.
l Command itself with special characters as a parameter. Some command line tools under Linux, such as (e) GREP, SED, AWK, FIND, and LOCATE, use their regular expression (Regular Expressions). These expressions are amazingly similar to pattern matching, but there is different in certain places.
However, in order to take these special commands, the shell cannot explain it as a pattern match:
Find. -name [1-9] * -print
Find: Paths Must Precede Expression
should be:
Find. -name '[1-9] *' -print
./346.bak
./124.bak
./583.bak
./311.bak ~
You can reference special characters by back slash, such as!, $, Or space:
ls /! *
! 56.bak
Or use (single) quotation mark:
Ls '!' *
! 56.bak
Please note what location should be placed clearly. Command ls '! *' Will look up the file named '! *', Because the wildcard is also between quarters, so it can only be explained according to the literal.
Output redirection
The concept of UNIX is to bring together many small programs, each with special expertise. Complex tasks are not completed by large software, but using the shell mechanism, combining many small programs together. The redirection plays an important role in it.
Redirect between multiple commands
This is to be identified by the pipeline symbols | PiPE. The syntax is:
Command1 | Command2 | Command3, etc.
This format you must have seen it. The pipelines often send the output of a program to 'more' or 'less' to read.
LS -L | Less
Where the first command provides a directory content, the second is displayed in a page page. More complex examples such as:
RPM -QA | GREP ^ X | LESS
The first command gives all installed RPM packets, and the second is filtered (Filter: 'grep'), only the package starting with '^ x', the third command will turn the result The way is displayed.
Redirect to file
Sometimes, you want to save the output of the command to the file or use the file content as the command of the command. This can be implemented by '>' and '<'. Command> file
Save the output of the Command to the file, which will override the contents of the file:
LS> Dirlist
Save the contents of the current directory to 'Dirlist' files.
Command Enter the file as a Command: Sort Send the contents of the file 'dirlist' to the command 'sort', and then send the sorted result to the file 'sdirlist'. Of course, you can also stand in one step: LS | Sort> SDIRLIST A special way is 'Command 2> File'. This is sent to the error information executed by Command to the file. This will need this time ... Another operator is '>>', which adds the output to the existing file: Echo "String" >> File Add String to the file file. This is a good way to complete the editor without opening the file! However, both '<' and '>' operators have an important limit: COMMAND Will delete the contents of File1, and Command But you can work very well, add the processing of File1 to the file. Is it a little bit? ;-) Don't panic, you can learn from your own speed, step by step. Don't forget, practice is the best way to learn ... After familiarizing with many shell mechanisms, you may have anxious to know how to customize the environment. In the following two, you will get this revelation. In the last one, there is a common question and answer (FAQ) that handles shell error information, and some configuration skills. Article 5: BASH configuration file / prompt / change $ PATH Bash configuration file Run in your home directory ls .bash * You will see these files: l.bash_history: Record the command you previously entered, l.bash_logout: When you exit the shell, the command to be executed, l .bash_profile: When you log in to the shell, the command to be executed, l.bashrc: When you open a new shell, the command to be executed. Note the difference between the next two: '. Bash_profile' is read once at the beginning of the session, and '.bashrc' is read each time you open a new terminal (such as a new XTERM window). According to the tradition, you have to define the variables, such as PATH, put it in '.bash_profile', and like the aliases and functions, it is placed in '.bashrc'. But because '.bash_profile' is often set to read the contents of '.bashrc', you will put all the configurations in '.bashrc' if you discharge. These files are settings for each user. The system level setting is stored in the file under '/ etc / profile', '/ etc / bashrc' and directory '/etc/profile.d'. But you are used to using your own profiles: Editing doesn't need 'root' permissions, but also makes your settings more personal. When the system level is conflicted with the user-level setting, the user settings will be employed. Read '.bashrc' content, if you want to save things, put all your configurations into '.bashrc'. These files above are the settings of each user, and the system level setting is stored in the files under '/ etc / profile', '/ etc / bashrc' and directory '/etc/profile.d'. You are best habit to use your respective profiles: Editing doesn't need 'root' permission, but also makes your settings more personality. When the system level is conflicted with the user-level setting, the user setting will be preferentially employed. Prompt Every time you open a console or xterm, the first thing you see is the prompt (Prompt), similar to: Account @ Hostname ~ $ Under the default setting, the prompt will display your username, the host name (default is 'localhost'), the current directory (in Unix, '~' means your home directory). According to the tradition, the last character can identify you is a normal user ($), or 'root' (#). You can set the prompt through the $ PS1 variable. command Echo $ PS1 The current setting will be displayed. Among them, the meaning of the characters is described in the 'PROMPTING' section of Man Bash. How can I complete the ideal settings? For forgetful beginners, the default setting is somewhat unfriendly, because the prompt only displays the last part of the current directory. If you see a prompt like this Tom @ localhost bin $ Your current directory may be '/ bin', '/ usr / bin', '/ usr / local / bin' and '/ usr / x11r6 / bin'. Of course, you can use PWD (output the current directory, Print Working Directory) Can you call Shell to tell you the current directory? of course can. Here I will mention the settings, including the prompt, mostly included in the file '/ etc / bashrc'. You can change the settings by editing the '.bash_profile' and '.bashrc' under the respective HOME directory. In the 'PROMPTING' section in Man Bash, these parameters have been described in detail. You can add some gadgets, such as the current time of different formats, historical record numbers, or even different colors. In '~ / .bashrc', I like the settings is: PS1 = "/ [/ 033 [1M /] [/ w] / [/ 033 [0m /]" The setting in 'root' in '~ / .bashrc' is: PS1 = "/ [/ 033 [0; 31M /] [/ w] / [/ 033 [0M /]" So what I get the prompt is: [/ usr / bin] When using 'root', becomes: [/ usr / bin] I have removed the host name and user name because I can't use it. But I can see that my identity is ordinary users or 'root'. Note that ordinary users' prompts can be black and white, or black words on white. To get the right color allocation on the terminal, you can edit the following script Color to give execution permissions (chmod x color), and then run. #! / bin / bash # # This file echoes a bunch of color cots to the # Terminal to Demonstrate What's Available. Each # line is the color code of one forground color, # out of 17 (default 16 escapes), FOLLOWED BY A # Test Uses of That Color on All Nine Background # Colors (Default 8 Escapes). # T = 'gyw' # The test text echo -e "/ n 40m 41m 42m 43m / 44M 45M 46M 47M "; For fgs in 'm' '1m' '30m' '1; 30m' 31m '' 1; 31M '32m' / '1; 32m' '33m' '1; 33M' '34m' 1; 34m '' 35m '1; 35m' / '36m' '1; 36m' 37m '1; 37m'; DO fg = $ {fgs // /} echo -en "$ fgs / 033 [$ fg $ t" FOR BG IN 40M 41M 42M 43M 44M 45M 46M 47M; Do Echo -en "$ EINS / 033 [$ FG / 033 [$ BG $ T / 033 [0M"; DONE Echo; DONE echo A more appropriate setting: PS1 = "/ u: / w // $" In this way, the prompt becomes: User_name: / usr / bin $ You can test different settings (for example, export ps1 = "/ u: / w // $") by command export. If you find your right prompt, you will be set to your '.bashrc' '. In this way, each time the console or terminal window is turned on, it will take effect. You can even set the subject (theme), which is equipped with different colors, which looks like a great OL. C64 prompt. If you are interested, you can look at it. Bashish http://hem.passagen.se/arnognulf/index2.html). Change $ PATH The '$ PATH' is the same as '$ ps1', and it is also an environment variable. enter set All currently defined environment variables will be listed. The environment variables you see are defined in the shell's profile, which may be a user's own profile, or the 'root' is defined by the system-level file under '/ etc'. If you use x, more variables will be configured by X, your window manager, or desktop environment. If you don't know these settings, you should not be able to change casually. Understanding how to change the $ PATH variable is useful, because this variable determines which directories will look for commands or programs. If you want to execute the directory in $ PATH, you don't have to enter the full path to this command, you can enter the command directly. Some third-party software does not place an executable in the standard directory of Linux. Therefore, add these non-standard installation catalogs to $ PATH is a solution. In addition, you will also see how to handle a general environment variable. First, as a convention, all environment variable names are capitalized. Since Linux is case sensitive, you have to pay attention. Of course, you can define some variables yourself, such as '$ PATH', '$ PATH', but shell will ignore these variables. The second point is that the variable name is sometimes starting with '$', but sometimes not. When setting a variable, you use the name directly without adding '$': PATH = / usr / bin: / bin: / usr / local / bin: / usr / x11r6 / bin To get a variable value, you will add '$' before the variable name: Echo $ PATH / usr / bin: / bin: / usr / local / bin: / usr / x11r6 / bin Otherwise, the variable name will be regarded as a normal text: echo path Path The third point to dealing with $ PATH variable is: You cannot replace only variables, but to add new strings to the original value. In most cases, you can't use 'path = / some / Directory' because it will delete all other directories in the PATH, so that you have to give a full path when you run the program. So, you can only add: PATH = $ PATH: / SOME / DIRECTORY In this way, PATH is set to the current value (represented in $ PATH) The newly added directory. So far, you only set a new $ PATH variable for the current terminal. If you open a new terminal, run the Echo $ PATH, return the old $ PATH value, and you can't see the new directory you just added. Because you are prior to defining a partial environment variable (only for the current terminal). To define a global variable, enabling a terminal in the later open, you need to output local variable output, you can use 'export' commands: export path = $ PATH: / SOME / DIRECTORY Now if you open a new terminal, enter Echo $ PATH, you can also see the new settings of the $ PATH. Note that the command 'export' can only change the variables in the current terminal and the subsequent terminal. There is no effect on the terminal already run. In order to add the directory permanent to your $ PATH, just add the line of 'export' to your '.bash_profile' file. Please do not set PATH in '.bashrc', otherwise it will cause accident growth in the PATH. Every time you open a new shell, '. Bashrc' will work. So if you add a directory in this file, you open a terminal each time, the directory will be added. This will cause the PATH variable due to directory replication, constantly grow. Chapter 6: Alias of the command, Shell function / from here / shell FAQ The alias of the command, the shell function Remember all commands and each belt, and then enter one by one, this is really a bit boring. But fortunately, you can define shortcuts for common commands. These shortcuts can be defined using a simpler command alias (alias), or the syntax of complex SHELL functions. Order alias For example, I use the following command to upload the files in MUO: Rsync-E SSH -Z -T -R -VV --Progress / Home / Tom / Web / MUO / RSMUO / DOCS MUO: / WWW / MANDRAKEUSER / DOCS Obviously, if you need to enter one by one, I will become a wood sooner. So I define an alias in '~ / .bashrc': Alias Upmuo = 'rsync -e ssh -z -t -r -vv --Progress / Home / Tom / Web / MUO / RSMUO / DOCS MUO: / WWW / MANDRAKEUSER / DOCS' Now, I can complete the upload task when I enter UPMUO. Defining the syntax of an alias is: Alias shringcut = 'command' If there is a space in the command, you need to use quotation marks (if you have spaces between commands and options). Please note that you can use single quotes or double quotes, but they are different. Single quotes will deprive all of these characters, and the '$' (parameter) and '`' (command replacement) in Dual quotation are exceptions. This means that if you want to apply variables or commands in the alias, you have to use double quotes. Look at the above example, I define a variable called MuoHome in '.bashrc': Export muohome = $ home / web / muo / rsmuo / docs To use this variable in the alias above, I must use double quotes: Alias Upmuo = "Rsync-E SSH -Z -T -R -VV --Progress $ MUOHOHOME MUO: / WWW / MANDRAKEUSER / DOCS" Otherwise, the alias will look up a directory or file called '$ muohome'. You can quickly create an alias in the command line, or put the command in your respective '~ / .bashrc', or put it in the system-level '/etc/profile.d/alias.sh' (while Mandrake Linux 8 In previous versions, use '/ etc / bashrc'). To delete an alias, just enter: UNALIAS Alias. Running Alias will list all the unique names in your system. If you look at '~ / .bashrc' and '/etc/profile.d/alias.sh', you will find that the system has defined some alias. You can define multiple individual names for the same command. Of course, you have to confirm the alias and other program names, such as Alias RM = 'ls -l', you can't work. You can enter these shortcuts on the command line and test it. If the shell can't find the same name, you can use it as an alias. The following alias may be useful (don't forget quotation marks!): l Alias rpmq = 'rpm -qa | grep': Now rpmq string will list all names of the installed RPM package, l Alias Ls = 'ls -ho - ket | more': LS will list files in color paging, file size is in KB, l alias us = 'du --max-defth = 1 | sort -n | more': Use the sub-directory is ranked in size and listed in paging, The alias of the directory can also be movable media: Alias DLM = '/ mnt / cdrom / redhat / rpms /'. Tip: There will be an alias that is similar to the beginning of the same letter, such as starting all the alias of all directories with 'd', which helps to memorize. I believe that you will use these features. Shell function Write the shell function involving the shell script, which beyond the scope of our discussion (not within my master scope ;-)). In fact, the shell function belongs to the shell script, but can be preloaded and executed under the same shell (and the general shell script is at least one Sub-shell). With the shell function, you can do a lot of things that Aliases can't do. Here is an example: Function Apros () {Apropos $ 1 | Egrep -V '(3 | / (N /)'; Define a new command called 'Apros'. Apros Name will first execute 'apropos name' (ie, search command in Man Page), then send the resulting output to the pipe (|), then use 'egrep' filtration, exclude Chapter '3' and the 'N' chapter Man Page, this command may have no big use, but can organize the output of 'apropos' command. The function allows you to use the runtime parameters inside the function. And alias, only allows a parameter to be taken within the command line (such as the alias 'rpmq'). '$ 1' is the positional parameter, indicating the position identifier of the first parameter of the function. In this type, there is also '$ 2', etc. Function Apros () {Apropos $ 1 | EGREP -V "/ ($ 2"; If you run the 'apros' command like this: Apros Name Man_SECTION_NUMBER This command will search for the Name of Man Pages in the title, but exclude the Man_SECTION_NUMBER section: Apros Menu 3 The title of the title containing 'Menu', but excludes the third chapter (regarding programming). Note that you have to quote twice, and it also uses double quotes: l You must reference the 'EGREP' search mode, which can not be misunderstood by the shell. l You must use double quotes so that second parameters can be explained correctly. l You must reference parentheses so that 'egrep' is paid to the object to treat parameters. Is it a bit mean? ;-) The hand of the shell function is similar to the alias: put it in your '.bashrc' file, so you can take effect. Depart from here We talk about only the beginning of the shell. Master the shell script, you can do a lot, such as automating task, correct the error in others' scripts, and customize Linux systems according to your habits. If you intend to learn some complicated programming language, the shell script is also a good start because the basic concept is similar. Bash Programming - Introduction How-to: http://www.ibiblio.org/mdw/howto/ba...ntro-howto.html Will deepen these topics and will bring you to the world of shell programming. Then you can continue to read the Advanced Bash-Scripting Guide I strongly Recommended. http://www.ibiblio.org/mdw/ldp/abs/....TML), the author is Mendel Cooper. If you prefer a paper book, I recommend S. Veraraghavan's "Teach Yourself Shell Programming", SAMS Press. I feel O'Reilly, "Learning the Bash Shell" written by Newham / Rosenblatt, but this may only look at me ;-). In addition to these, practice, practice, and practice. Read the shell script written by others to see what they are doing, how to do it, why do you do it. Please don't test your script with 'root'. Have fun.