How shell works in Linux

Source: Internet
Author: User
Tags echo command


Shell is the interface between the user and the Linux operating system. There are multiple shells in Linux, where Bash is used by default. This chapter describes how shell works, shell types, General Shell operations, and bash features. Shell is the interface between the user and the Linux operating system. There are multiple shells in Linux, where Bash is used by default. This chapter describes how shell works, shell types, General Shell operations, and bash features.
What is shell?
As the operating system shell, the Linux system shell provides users with the interface to use the operating system. It is a general term for the command language, command Interpretation Program, and programming language.
Shell is the interface program between the user and the Linux kernel. If you think of the Linux kernel as the center of a sphere, shell is centered around the outer kernel. When commands are passed from shell or other programs to Linux, the kernel will respond accordingly.
Shell is a command language interpreter. It has its own built-in shell command set, and shell can also be called by other applications in the system. The commands entered by the user at the prompt are explained by Shell and then passed to the Linux core.
Some commands, such as changing the working directory command CD, are included in the shell. There are also some commands, such as Copy command CP and move command RM, which are separate programs in a directory in the file system. For users, there is no need to worry about whether a command is built inside shell or a separate program.
Shell first checks whether the command is an internal command. If it does not check whether it is an application (the application here can be a Linux utility, such as LS and RM, it can also be a purchased commercial program, such as XV, or a free software, such as Emacs ). Then shell searches for these applications in the search path (the search path is a list of directories where executable programs can be found ). If the entered command is not an internal command and the executable file is not found in the path, an error message is displayed. If the command is successfully found, the internal command or application will be decomposed into system calls and passed to the Linux kernel.

Another important feature of shell is that it is an interpreted programming language. The shell programming language supports most program elements that can be seen in high-level languages, such as functions, variables, arrays, and program control structures. The shell programming language is easy to learn. Any command that can be typed in a prompt can be put into an executable shell program.
When a common user successfully logs on, the system executes a program called shell. The shell process provides a command line prompt. As the default value (BASH is the default shell in turbolinux), "$" is used as a prompt for common users and "#" is used as a prompt for Super Users (Root.
Once a shell prompt appears, you can enter the command name and the parameters required by the command. Shell will execute these commands. If a command takes a long time to run, or a large amount of output is generated on the screen, you can press Ctrl + C on the keyboard to interrupt it (stop it before it ends normally ).
When you are about to end the Login Dialog process, you can enter the logout command, the exit command, or the file terminator (EOF) (press Ctrl + D to complete the logon.
Let's take an internship on how shell works.
$ Make work
Make: *** no rule to make target 'work'. Stop.
$
Note: make is the name of a command in the system, followed by the command parameters. After receiving this command, shell will execute it. In this example, the system returns information and stops executing the command because the input command parameters are incorrect.
In the example, shell looks for a program named make and runs it with the work parameter. Make is a program that is often used to compile large programs. It is compiled with parameters as the goal. In "make work", the goal of make compilation is work. Because make cannot find the target named "work", it will give an error message indicating that the operation failed, and the user will return to the system prompt.
In addition, if the shell cannot find the program named by the command name after you type the command line, an error message is displayed. For example, if you type:
$ Myprog
Bash: myprog: Command not found
$
You can see that the user gets an error message that does not find the command. After the user knocks on the wrong command, the system generally gives such an error message.
Shell Type
There are multiple types of Shell in Linux, the most common of which are Bourne shell (SH), c shell (CSH), and Korn shell (Ksh ). Each of the three shells has its own advantages and disadvantages. The Bourne shell is the original shell used by UNIX and can be used on every UNIX. Bourne shell is excellent in shell programming, but it is inferior to several other shells in dealing with user interactions. The default shell in Linux is the Bourne again shell, which is an extension of the Bourne shell, bash for short. It is completely backward compatible with the Bourne shell, in addition, many features are added and enhanced based on the Bourne shell. Bash is placed in/bin/bash. It has many features, such as command completion, command editing, and command history table, it also contains many advantages of C shell and Korn shell, flexible and powerful programming interfaces, and friendly user interfaces.
C shell is a shell that is more suitable for programming than the Bourne shell. Its syntax is similar to that of C. Linux provides tcsh for people who like to use C shell. Tcsh is an extension of C shell. Tcsh includes command line editing, programmable word completion, spelling correction, historical command replacement, Job control, and syntax similar to C language. It is not only compatible with bash shell as a prompt, it also provides more prompt parameters than bash shell.
The Korn shell combines the advantages of C shell and Bourne shell and is fully compatible with the Bourne shell. Linux provides pdksh (Ksh extension), which supports task control and can be suspended on the command line, executed in the background, awakened, or terminated.
Linux does not neglect other shell users, but also includes some popular shell such as ash and zsh. Each shell has its purpose. Some shells have patents, and some can be obtained from the Internet or other sources. To decide which Shell to use, read the online help of various shells and try it out.
When you log on to Linux, the/etc/passwd file determines the shell to use. For example:
# Fgrep Lisa/etc/passwd
Lisa: X: 500: 500: turbolinux User:/home/LISA:/bin/bash
Shell is listed at the end of each row (/bin/bash ).
As Bash is the default shell in Linux, this chapter mainly introduces bash and its related knowledge.
Shell commands
Command Line c
When you log on to Linux, you can see a shell prompt that marks the start of the command line. You can enter any commands and parameters at the end of the prompt. For example:
$ Date
November 23 01:34:58 CST 1999
$
When a user logs on, the user actually enters the shell, which follows certain syntax to explain the input command and pass it to the system. The first word entered in the command line must be the name of a command, and the second word is the option or parameter of the command. Each word in the command line must be separated by spaces or tabs. The format is as follows:
$ Command Option arguments
1. Options and Parameters
The option is a code that contains one or more letters. It has a minus sign (minus sign is necessary, and Linux uses it to differentiate options and parameters ), option can be used to change the type of the command execution action. For example:
$ Ls
Motd passwd
$
This is an LS command without any options. It can list all files in the current directory, only the names of each file are listed, and no more information is displayed.
$ LS-l
Total 2
-RW-r-2 wzh book 22 Apr 20 20:37 motd
-RW-r-2 wzh book 796 APR 20 20:37 passwd
$
The-L option is added to list a row of information for each file, such as the data size and the last modification time.
Most commands are designed to accept parameters. A parameter is one or more words entered after the option in the command line, for example:
$ LS-l text
-RW-r-2 wzh book 22 Apr 20 20:37 motd
-RW-r-2 wzh book 796 APR 20 20:37 passwd
$
All files in the text directory and their information are displayed.
Some commands, such as LS, can contain parameters, while some commands may require some minimum parameters. For example, the CP Command requires at least two parameters. If the number of parameters does not meet the command requirements, shell will provide error information. For example:
$ CP-I mydata newdata
Note: Options in the command line are input prior to parameters.
2. Command Line Features
The command line is actually a text buffer that can be edited. You can edit the input text before pressing enter. For example, you can use the backspace key to delete the just-typed characters, delete the entire line, and insert characters so that when you enter commands, especially complex commands, an error occurs, you do not need to re-enter the entire command. You can use the edit operation to correct the error.
The up arrow can be used to re-display the just-executed command. This function can be used to re-execute the previously executed command without re-typing the command.
Bash stores the list of commands you have previously typed. This list is called the command history table. Click the arrow to display each command on the command line. Similarly, you can move the down arrow in the command list to display previous commands on the command line. You can modify and execute these commands. This feature is described in detail in section 10.4.
Multiple commands can also be placed in a command line and separated by semicolons. For example:
$ LS-F; CP-I mydata newdata
You can also enter a command in several command lines and use a backslash to continue a command line to the next line.
$ CP-I
Mydata
Newdata

The preceding CP command is input in three lines. The first two lines end with a backslash and use the three lines as one command line.
Special characters in Shell
In shell, in addition to common characters, you can also use special characters with special meanings and functions. When using them, pay attention to their special meanings and scopes. The following describes these special characters.
1. wildcard
Wildcard characters are used for pattern matching, such as file name matching, path name search, and string search. Common wildcard characters include *,? And the Character Sequence enclosed in square brackets. You can include These wildcards in the file name of the command parameter to form a so-called "pattern string" and perform pattern matching during execution.
* Represents any string (length can be different). For example, "F *" matches any string that starts with "F. However, it should be noted that the dot (.) before the file name must be explicitly matched with the slash (/) in the path name. For example, "*" cannot match. file, but ". *" can match. File.
? Represents any single character.
[] Indicates a specified character range. As long as the character at the [] position in the file name is within the range specified by [], the file name matches the pattern string. The character range in square brackets can be composed of directly given characters, and can also be composed of starting characters, ending characters, and intermediate hyphens (-) that indicate a limited range. For example, F [a-d] and f [ABCD] serve the same purpose. Shell uses all file names that match the pattern string specified in the command line as command parameters to form the final command and then execute the command.
Table 10-1 describes the meanings of these wildcards.
Table 10-1 wildcard meanings
Mode string
Yi
*
The names of all files in the current directory.
* Text *
The names of all files with text in the current directory.
[AB-DM] *
Names of all files starting with A, B, C, D, and m in the current directory.
[AB-DM]?
The names of all files starting with A, B, C, D, and m in the current directory, followed by only one character.
/Usr/bin /??
The names of all files with two characters under the/usr/bin directory.
Note that the hyphen "-" is only valid in square brackets and indicates the character range. For example, it becomes a common character outside square brackets. And * and? Wildcards are used only outside square brackets. If they appear in square brackets, they also lose the wildcard capability and become common characters. For example, the mode "-A [*?] Only one pair of square brackets in ABC is a wildcard, * and? Are common characters. Therefore, the matching strings can only be-a * ABC and-? ABC.
Finally, we will explain some issues that need to be paid attention to when using wildcards. Because *,? And [] are of special significance for shell, so these characters should not appear in normal file names. In particular, do not show them in the directory name; otherwise, the shell matching may be infinitely recursive. In addition, if the directory does not contain a file name that matches the specified mode string, shell will use this mode string as a parameter to send it to related commands. This may be the reason for the special characters in the command.
2. quotation marks
In shell, there are three types of quotation marks: single quotation marks, double quotation marks, and reverse quotation marks.
* Single quotes'
All characters enclosed by single quotes are common characters. When special characters are enclosed in single quotes, they will also lose their original meaning and will only be interpreted as common characters. For example:
$ String = '$ Path'
$ Echo $ string
$ Path
$
It can be seen that $ retains its meaning and appears as a common character.
* Double quotation marks
Characters enclosed by double quotation marks, except the characters $, ', and' are still special characters and keep their special functions. Other characters are still treated as common characters. For $, it is to replace this variable and $ with the variable value specified later. For, it is an escape character, it tells shell not to perform special processing on the character after it, just as a common character. You can imagine that the double quotation marks must be preceded by only four characters $, ', and. If no signature is added before the signature, shell matches the signature with the previous signature.
For example, assume that the path value is.:/usr/bin:/bin and enter the following command:
$ Teststring = "$ path" $ path"
$ Echo $ teststring
.:/Usr/bin:/bin "$ path
$
You can try the result by yourself without adding the second double quotation mark.

* Backticks'
The key corresponding to the character ') is generally located in the upper left corner of the keyboard. Do not confuse it with single quotation marks. The string enclosed by backquotes is interpreted by shell as a command line. During execution, shell first executes the command line, and replaces the entire anti-quotation mark (including two anti-quotation marks) with its standard output result. For example:
$ Pwd
/Home/XYZ
$ String = "current directory is 'pwd '"
$ Echo $ string
Current directour is/home/XYZ
$
When shell executes the echo command, it first executes the command PWD in 'pwd', replaces the output result/home/XYZ with the 'pwd', and finally outputs the entire replaced result.
This function can be used to replace commands. That is, the execution result enclosed by backquotes is assigned to the specified variable. For example:
$ Today = 'date'
$ Echo today is $ today
Today is Mon Apr 15 16:20:13 CST 1999
$
Backquotes can also be nested. However, you must note that the inner anti-quotation marks must be escaped using a backslash () When nesting. For example:
$ Abc = 'echo the number of users is 'Who | WC-l"
$ Echo $ ABC
The number of users is 5
$
Shell special characters can also be used in the command line between backquotes. Shell is used to get the result of the "in" command. It actually needs to execute the command specified in. Special characters in the command, such as $ ,",? And so on, and "The contained can be any legal shell command, such:
$ Ls
Note readme.txt notice Unix. dir
$ Teststring = "'echo $ home' 'ls [NN] * '"
$ Echo $ teststring
/Home/yxz note notice
$
In other cases, you can try it yourself.
1. annotator
In shell programming, you often need to comment on some body lines to increase the readability of the program. In shell, the text line starting with the character "#" represents the comment line.
In addition, there are some special characters such as: for input/output redirection and pipeline,> and |; for executing background commands &; the Command Execution operators & | and {} representing the Command Group are described in the following sections.
Standard Input/Output and redirection
1. Standard Input and Output
We know that when executing a shell command line, three standard files are automatically opened, namely the standard input file (stdin), which usually corresponds to the keyboard of the terminal and the standard output file (stdout) and stderr, both of which correspond to the terminal screen. The process obtains the input data from the standard input file, outputs the normal output data to the standard output file, and sends the error information to the standard error file.
Take the cat command as an example. The function of the cat command is to read data from the file provided by the command line and send the data directly to the standard output. Use the following command:
$ Cat config
The file config content is displayed on the screen in sequence. However, if Cat has no parameters in the command line, it reads data from the standard input and sends it to the standard output. For example:
$ Cat
Hello World
Hello World
Bye
Bye

$
Each line entered by the user is immediately output to the screen by the cat command.
In another example, the command sort reads the file body by line (when the file name is not given in the command line, it indicates reading from the standard input), sorts it, and sends the result to the standard output. The following example reads a purchase order from the standard input and sorts it.
$ Sort
Bananas
Carrots
Apples

Apples
Bananas
Carrots
$
At this time, we got the sorted purchase order on the screen.
Directly using standard input/output files has the following problems:
When the input data is input from the terminal, the user spends only one time to input the data. The next time you want to use the data, you have to input it again. In addition, it is not convenient to modify input errors on the terminal.
The information output to the terminal screen can only be viewed. We cannot process the output more, for example, using the output as the input of another command.
To solve these problems, the Linux system introduces two other mechanisms for input/output transmission, namely, input/output redirection and pipelines.
2. input redirection
Input redirection refers to redirecting standard input of commands (or executable programs) to a specified file. That is to say, the input can come from a specified file instead of the keyboard. Therefore, input redirection is mainly used to change the input source of a command, especially those that require a large amount of input.
For example, run the WC command to count the number of lines, words, and characters in a specified file. If you only type:
$ WC
WC will wait for the user to tell it what to calculate, and shell will appear as if it were dead. All the text typed from the keyboard will appear on the screen, but there is no result, WC did not write the command result on the screen until you press <Ctrl + D>.
If a file name is given as a parameter of the WC Command, as shown in the following example, WC returns the number of lines, words, and characters contained in the file.
$ WC/etc/passwd
20 23 726/etc/passwd
$
Another way to pass the content of the/etc/passwd file to the WC command is to redirect the WC input. The general format of input redirection is: Command this text forms the content
> Of the here document, which
> Continues until the end
> Text delimter
> Delim
4 17 98
In the file name. For example:
$ Ls> directory. Out
$ Cat directory. Out
Ch1.doc ch2.doc ch3.doc chimp config mail/test/
$
Save the LS command output as a file named directory. Out.
Note: If the file behind the symbol already exists, the file will be overwritten.
To prevent the specified file in the output redirection from storing only the output redirection content of the current command, shell provides an append Method for output redirection. The output append redirection function is very similar to the output redirection function. The difference is that the output result of the command (or executable program) is appended to the end of the specified file, the original content of the file is not damaged.
If you want to append the output result of a command to the end of a specified file, you can use the append redirection operator>. Format: Command> file name. For example:
$ Ls *. Doc> directory. Out
$ Cat directory. Out
Ch1.doc ch2.doc ch3.doc chimp config mail/test/
Ch1.doc ch2.doc ch3.doc
$
Like the standard output redirection of a program, the program's error output can also be reoriented. Use symbol 2> (or append symbol 2>) to redirect the error output device. For example, the following command:
$ Ls/usr/tmp 2> err. File
You can see the normal output results of the program on the screen, but send any program error information to the file err. file for future checks.
You can also use another output redirection operator (&>) to send both standard output and error output to the same file. For example:
$ Ls/usr/tmp &> output. File
By combining commands with redirection, You can implement new functions that a single system command cannot provide. For example, use the following command sequence:
$ Ls/usr/bin>/tmp/Dir
$ WC-W

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.