What is shell? Shell is an interface program between you (users) and Linux (or more accurate, are you and Linux kernel). Each command you entered at the prompt is explained first by the shell and then pass it to the Linux kernel.
Note: If you are familiar with M $ -DOS, you will recognize this very much between DOS users and Command.com. The only difference is that the function of Command.com is far from being compared with the shell.
The shell is a command language interpreter (COMMAND-LANGUAGE Interpreter). Have your own built-in shell command set. In addition, the Shell can also be called by other effective Linux utilities and applications in the system.
Whenever you type a command, it is explained by the Linux Shell. Some commands, such as printing the current work directory command (PWD), is included in Linux Bash (in the internal command of DOS). Other commands, such as copy command (CP), and mobile command (RM), are separate programs existing in a directory in the file system. For users, you don't know (or may not care) an order is built within the shell or a separate program.
Shell first checks if the command is internal command, not check whether it is an application, the application here can be the utility of Linux itself, such as LS and RM, or buy business programs, such as XV, or public Public Domain Software, just like GhostView. The shell is then trying to find these applications in the search path. Search path is a list of directory that can find executable programs. If you type it is not an internal command and does not find this executable file in the path, an error message will be displayed. If the command is found successfully, the SHELL internal command or application will be decomposed into a system call and pass it to the Linux kernel.
Another important feature of the shell is that it itself is an interpreted programming language. The shell programming language supports the vast majority of program control structures that can be seen in advanced languages, such as loops, functions, variables, and arrays. Shell programming language is easy to learn, and once it masters it, it will become your strength tool. Any command that can be typed in a prompt can also put it in an executable shell program, which means that a task can be easily repeated with the shell language.
How to start the shell after you successfully log in into the system, and always use the interaction with the system kernel until you exit the system. Each user on your system has a default shell. The default shell of each user is specified in the passwd file in the system, the path of the file is / etc / passwd. There is anything else in the Passwd file: each person's user ID number, a password encrypted copy and the program executed immediately after logging in, (Note: In order to strengthen security, the current system generally puts the encrypted password Another file - Shadow, and the part of the password in Passwd is replaced by an X-character) Although this program must be a Linux Shell, most of the cases.
The most common shell can use a variety of different shells in Linux and UNIX systems. Several of the most commonly used Bourne Shell (SHELL (CSH), and Korn Shell (KSH). Three shells have their advantages and disadvantages. The Bourne Shell is the Steven Bourne. It is the original use of UNIX and can be used on each UNIX. Bourne Shell is quite excellent in shell programming, but it is not as good as many other shells in handling and user interaction. C shell Written by Bill Joy, which considers the friendlyness of the user interface. It supports features that are not supported by the Bourne Shell such as a command complement (COMMAND-LINE Commention). It is not as good as Bourne Shell, but C shell is used by many C programmers because C shell's syntax and C language are very similar because C shell's syntax and C language are also the origin of the c shell name. Korn Shell (KSH) is written by Dave Korn. It combines the advantages of C shell and Bourne Shell and is fully compatible with the Bourne Shell.
In addition to these shells, many other shell programs have absorbed the advantages of these original shell programs and become new shells. There is a common TCSH (CSH extension), Bourne Again Shell (extension of PDKSH, KORN SHELL (Extension of PDKSH, KORN SHELL). Bash is the default shell of most Linux systems.
The Bourne Again Shell Bourne Again Shell (BASH), as its name is implied, is the expansion of the Bourne Shell. Bash is fully compatible with Bourne Shell and has increased and enhanced many features on the basis of Bourne Shell. BASH also includes the advantages of many C and Korn shells. Bash has a very flexible and powerful programming interface, while also a very friendly user interface.
Why use BASH instead of SH? The biggest disadvantage of Bourne Shell is that it handles users' input. Type commands in the Bourne Shell, especially when you type a lot of similar commands. BASH prepared several features make the command input easier.
Command-line completion, you don't have to enter the command to enter the command to entroy when you enter the command. For example, assume that the current working directory contains the following files and subdirectories:
News / bin / games / mail / samplefile test / if you have to enter the Test subdirectory, you will enter the following command: CD test This command can meet your needs, but BASH also provides a slightly different way to complete the same thing. Because Test is the only subdirectory in the current directory, Bash can judge what you want after you only enter the letter t: CD T After you type the letter, the only possibility is Test. Want Bash to help you end the command, press the Tab button: CD T When you do this, Bash will help you upset the command and display it on the screen. But when you press the Enter key, the command is not executed, BASH will make you inspected whether the commands are really needed. When you enter a short command like this, you may not see its value, and even simply enter the speed in the command, but when you want to enter the command, you will find how this feature is. Beautiful. But what happens when there is more than one file that starts with the letter T? There is a problem when you use the command to make up, let's take a look at the situation below, the current directory has the following: news / bin / mail / samplefile test / Tools / Working / now have two in the directory document. Suppose you still want to enter the Test subdirectory, how to use the command to make up? If you type it like previous: CD T Bash will not know which subdirector you want to enter, because the information given is not unique. If you do this, Bash will send a buzz to remind you that there is not enough information to make up your command. Beep, Bash does not change the input command, which will enable you to enter more information on the original basis. In this example, you only need to type an E and press the Tab button, then Bash There is enough information to complete your command: CD test When you enter the command, you will try to make up the command to make up the command. If you don't want to make a beep, you need more Many information. You need to type more characters and press the Tab key again to repeat this process until you expect. The method of the wildcard is made to make the command input easier to use wildcards in the command. Bash supports three wildcards:
* Match any character and any number of characters
? Match any single character
[...] matches any single character contained in parentheses
* The use of wildcards Some icon commands are complement. For example, assume that the current directory contains the following files:
News / bin / Games / Mail / Samplefile TEST /
If you want to enter the Test directory, you will type CD test, or you want to use the command to make up:
CD T
There is now a third method to do the same thing. Because only one file begins with the letter T, you can use * wildcards to enter this directory. Type the following command:
CD T *
* Match any characters and any number of characters, so the shell will replace T * with Test (the only file that matches the unique and wild schemes in the current directory). There is only one file in the current directory to start with the letter T, this will be reliable. However, if there is more than one file in the current directory, the shell will try to enter the first directory that meets the matching scheme. This directory is the first directory sorted alphabet, this directory may be Maybe not what you expect. of.
A more practical use of wildcard * is a file similar to multiple names you want to execute. For example, assume that the current directory contains the following files:
CH1.DOC Ch2.Doc Ch3.Doc Chimp Config Mail / TEST / TOOLS /
If you need to print all extensions, you can use the simplified command like this:
lpr * .doc
In this example, BASH will replace * .doc to match all file names in the current directory that meets the works. This command will be processed after BASH.
Lpr ch1.doc ch2.doc ch3.DOC
The LPR command will be called as ch1.doc, ch2.doc, and ch3.doc as parameters.
Note: In addition to the examples given above, there are several ways: lpr * doc Lpr * OC Lpr * C
Wildcards? In addition to only match a single character, other functions are the same as wildcard *, if you use wildcard characters to print all extensions in the previously mentioned file, type the following command:
LPR ch? .doc
Wildcard [...] can match the characters or character range given in parentheses. Similarly to the previous directory as an example, print all extensions in that directory is the file file, you can type one of the following commands:
LPR CH [123] .doc
or:
LPR CH [1-3] .doc
The input redirection input is redirected for changing the input source of a command. Some commands need to enter enough information in the command line to work. For example, RM, you must tell RM in the command line you want to delete the file. Other commands require more detailed input, and the input of these commands may be a file. For example, the command WC statistics enters the number of characters, words, and number of lines in the files in the file. If you only type WC on the command line, WC will wait for you to tell it what to count, then Bash is like a dead, you typed everything appears on the screen, but what will not happen. This is because the WC command is collecting input for ourselves. If you press Ctrl-D, the result of the WC command will be written on the screen. If you enter a file name to do parameters, like the following example, WC will return the number of characters, words, and number of lines included in the file: WC Test
11 2 1
Another way to pass the Test file content to the WC command is to redirect the input of the WC. WC 11 2 1 Input redirects are not frequently used because most commands specify the file name of the input file on the command line in the form of parameters. Despite this, when you use a command that does not accept the file named input parameters, the input required is in an existing file, you can use the input redirection to solve the problem. Output redirection output redirection is more common than input redirects. The output redirect allows you to redirect an output of a command to a file instead of display on the screen. This feature can be used in many cases. For example, if there is a lot of output, you can't display it on the screen. You can redirect it into a file, and then use the text editor to open this file later; when you want to save the output of a command This method can be used. Also, the output redirection can be used to use the output of a command as the input of another command. (There is also a simpler way to use the output of a command as an input to another command, that is, use the pipeline, the use of the pipeline will be referred to the use of the output redirection of the "pipe" section) output redirection Similar, but the output redirected symbol is>. Note: The best way to remember the input / output redirect symbol is to point it as a funnel, and the funnel points point to the command you need to enter (because the command you need to accept will be at the left hand side), and put> A big mouth points to the funnel of the command with the output. Redirect an example, when you want to save the output of the ls command as a file named Directory.out, you can use the following command: LS> Directory.out The pipe pipe can connect a series of commands. This means that the output of the first command is passed to the second command through the pipe, and the output of the second command will be used as the input of the third command. The output of the last command in the pipeline will only appear on the screen (if the output redirection is used in the command line, it will be placed in a file). You can build a pipeline by using pipelines |, the following example is a pipeline: CAT Sample.Text | GREP "High" | WC -L This pipe will give the output of the CAT command (listed on the content of a file) to the GREP command. The grep command finds the word high in the input, the output of the grep command is all rows that contain the word high, and this output is sent to the WC command. The WC command with the -l option will count the number of rows in the input. Suppose the contents of Sample.txt are as follows: THINGS to do Today: Low: Go Grocery Shopping High: Return Movie HIGH: CLEAR Level 3 in Alien vs. Predator Medium: Pick Up Clothers from Dry Cleaner The pipeline will return results 2, pointing out that there are two important things you have today: CAT Sample.Text | GREP "High" | WC -L 2 The prompt BASH has two-level user prompt. The first level is the prompt that you often see when waiting for the command to enter. The default level prompt is a character $ (if it is a super user, ##). You can change your default prompt by changing the value of the BASH PS1 variable, for example: PS1 = "please enter a command" Take the Bash Shell's prompt to the specified string. When BASH looks forward to entering more information to complete the command, the second level prompt is displayed. The default second level prompt is>. If you want to change the second level prompt, you can implement the value of the PS2 variable: PS2 = "I NEED more INFORMATION" In addition, you can define your prompt with a special character, and the list below lists the most common special characters. The prompt special character code characters meaning /! Displays the history number of the command. / # Display the command number of the current command. / $ The display is as a prompt. If the user is root, the ## is displayed. // Show a backslash. / D shows the current date. / H Shows the host name. / N Print a new line. / NNN displays an octave value of NNN. / S shows the name of the current running shell. / t shows the current time. / u Shows the username of the current user. / W Displays the name of the current work directory. / w Displays the path to the current work directory. These special characters can be combined into many useful prompt programs (can also be combined into a very singular scheme), such as setting PS1: ps1 = "/ t" This causes the prompt to display the current time, just like the display below (the prompt will not have spaces): 02:16:15 And below: PS1 = / T Will cause the prompt to become the following: t This shows the importance of setting the quotation marks, the following prompt string: PS1 = "/ t //" Will make the prompt look like this: 02: 16: 30 / In this case, there will be a space behind the prompt because there is a space in quotation marks. Job Control Operation Control Controls the behavior of the current running process. In particular, you can hang a running process and restore its operation later. BASH keeps track of all launched processes, you can hang it or turn it to run in any time of a running process. Press CTRL-Z to hang a running process. The bg command enables a hang-haunken process to restore running in the background, and the counter FG command enables the process to recover run at the front desk. These orders are often used when the user wants to run it in the background. When a command is run at the front desk, it disables the user from interacting with the shell until the command ends. This usually doesn't cause trouble, because most commands have been implemented soon. If you want to run the command to spend a long time, we usually put it in the background so that we can continue to enter other commands at the front desk. For example, you enter this command: Command Find / -Name "Test"> Find.out It will look for files named TEST over the file system and save the results in a file called fing.out. If you run at the front desk, depending on the size of the file system, your shell will have few seconds or even minutes, you don't want this, you can enter the following content: Control-z BG The Find command is first hang, and then continue to be executed in the background, and you can go back to BASH immediately. User-friendly Configuration Bash This article has been described in many ways to configure BASH. But know now, what we do is only valid under the current running Bash. Once the system exits the system, all changes have also disappeared. In order to save these customized configurations, you must save them to an initialization file of a BASH. You can put any commands that you want to perform every time you enter Cash in the initialization file. The most common commands in this file are usually an initialization of Alias and variables. Bash's initialization file is called Profile. Every user who uses Bash has a .profile file in his user directory. BASH reads this file at each startup and performs all included commands. The following code is the content of the default .profile file. The location of this file is in the / etc directory. If you want to set your own bash, you will copy it to your user directory (if you haven't yet,) and name it .profile. Note: Some SETUP programs will automatically put a copy of a .profile file in your user directory when establishing a user. But not all do this, so it's best to check your user directory. Remember all files starting with a sentence, which is implied, only listed in ls -a or ls -a commands. # Commands Common To All logins EXPORT OpenWinHome = / usr / openwin Export minicom = "- c on" Export manpath = / usr / local / man: / usr / man / preformat: / usr / man: / x11 / man: / usr / openwin / m anexport hostname = "` CAT / etc / hostname` Path = "$ PATH: / USR / X11 / BIN: $ OpenWinHome / bin: / usr / games :." Less = -mm # I Had problems Using 'Eval test' instead of 'term =' 'IF ["$ TERM" = "" -o "$ term" = "unknown"]; then Term = Linux # Ps1 = '' Hostname ':' pwd '# ` IF ["$ shell" = "/ bin / pdksh" -o "$ shell" = "/ bin / ksh"]; then PS1 = "! $" Elif ["$ shell" = "/ bin / zsh"]; then PS1 = "% m:% ~% #" Elif ["$ shell" = "/ bin / ash"]; then PS1 = "$" Else PS1 = '/ h: / w / $ ` Fi PS2 = '> ` Ignoreeof = 10 Export Path Display Less Term PS1 PS2 ignoreeof Umask 022 # set up the color-ls environment variables: IF [$ shell "=" / bin / zsh "l; then Eval 'Dircolors -z' Elif ["$ shell" = "/ bin / ash" l; then Eval 'Dircolors -s' Else Eval 'DIRCOLORS -B' Fi echo Fortune echo Export Tape = "/ dev / nftape" Bash command summary This is a few most useful Bash internal commands: Alias: Set the Bash alias. BG: Make a hang-haunted process continues to execute in the background. CD: Change the current working directory. EXIT: Terminate the shell. EXPORT: Make the value of the variables to all the sub-process of the current shell. FC: The command used to edit the list of history commands. FG: Make a hang-haunged process continues to execute at the front desk. Help: Displays help information for the BASH internal command. Kill: Terminate a process. PWD: Displays the current work directory. Unalias: Delete the defined alias. Bash has many commands, but these are most common, want to know more detailed, please refer to Bash's Manual - Type Man Bash at the prompt. Bash Variables here are several most useful Bash variables, including variable names and brief description. Editor, Fcedit: The default editor of the BSAH FC command. Histfile: Files used to store historical commands. Histicsize: The size of the history command list. HOME: The current user's user directory. OldPWD: The previous working directory. PATH: BASH looks for the search path for executable. PS1: The command line level prompt. PS2: The secondary prompt of the command line. PWD: The current work directory. Seconds: The number of seconds passing after the start of the shell.