from the programmer's point of view, the shell itself is a program written in C language,From the user's point of view, the shell is a bridge between the user and the Linux operating system. The user can either enter command execution or use shell scripting to do more complicated operations. In the increasingly perfect Linux GUI today, in the field of system management, shell programming still plays a role that can not be ignored. An in-depth understanding and proficiency in shell programming is one of the required lessons for every Linux user.There are many types of shell in Linux, common: Bourne shell (/usr/bin/sh or/bin/sh), Bourne Again Shell (/bin/bash), C Shell (/usr/bin/csh), K Shell (/usr/bin/ksh), Shell for Root (/sbin/sh), and so on. Different shell language syntax differs, so it cannot be exchanged for use. Each shell has its own characteristics, and basically, mastering either of these is enough. In this article, we focus on bash, the Bourne Again Shell, which is widely used in daily work due to ease of use and free, and bash is the default shell for most Linux systems. In general, people do not differentiate between the Bourne shell and the Bourne Again shell, so in the following text we can see #!/bin/sh, which can also be changed to #!/bin/bash. A shell is a program with special functions that is an interface between the user and the core program (kernel) of the Unix/linux operating system. Why do we say that the shell is an intermediary between the system core program and the user? Readers who have read the introduction to the operating system know that the operating system is a system Resource manager and assigns, when you have the demand, you have to ask the system, from the operating system point of view, it must also prevent users from the wrong operation caused by the system damage? It is well known that commands or programs are ordered through a computer, and the program has a compiler (compiler) to turn the program into binary code, but what about the command? In fact, the shell is also a program, which is read by the input Device command, and then to the computer can understand the mechanical code, and then execute it.
Various operating systems have its own shell, Dos for example, its shell is command.com files. As with DOS, Ndos,4dos,drdos and other command interpreter programs can replace the standard Command.com, UNIX under the Bourne Shell (/bin/sh) and the C Shell (/bin/csh), Korn Shell (/ Bin/ksh), Bourne again Shell (/bin/bash), Tenex C shell (tcsh) ... Wait for the other shell. Unix/linux separate the shell from the core program, making it like a generic application that can be modified, updated, or added with new functionality without affecting the operating system itself.
Activation of the Shell
When the system is started, the core program is loaded with memory, which is responsible for managing the system until the system shuts down. It establishes and controls the processing program, manages the memory, the file system, the communication and so on. Other programs, including shell programs, are stored on disk. The core program loads them into memory, executes them, and cleans up the system after they are aborted. The Shell is a utility that is started when you check it in. By interpreting the commands entered by the user (by command line or command file), the Shell provides the function of the user and the core program to generate a conversation.
When you check in (login), a chat-style shell follows the trigger and prompts you to enter a command. After you type a command, then the shell works, and it does:
1. Parse the command column.
2. Handle universal characters (wildcards), Steering (redirection), pipelines (pipes), and work control (job controls).
3. Search and execute the command.
When you start learning unix/linux systems, most of your time is spent executing commands under the cue sign (prompt).
If you often enter a set of commands in the same form, you might want to automate those tasks. So, you can put some commands into a file (called a command file, script), and then execute the file. A shell command file is much like a DOS batch file (such as Autoexec.bat): it stores a sequence of UNIX commands in a file and executes the file. More sophisticated command files also support a number of modern programming language control structures, such as the ability to do conditional judgment, circulation, file testing, transmission parameters and so on. To write a command file, not only to learn the structure and skills of the program design, but also for the Unix/linux utility and how to operate need to have in-depth understanding. Some utilities are very powerful (such as grep, SED, and awk), and they are often used in command files to manipulate command output and files. After you become familiar with those tools and programming structures, you can start writing command files. When you execute a command from a command file, you are already using the shell as a programming language.
To elaborate on the life of the Shell
The first significant, standard Unix shell was V7 (seventh edition) UNIX, which was presented at the end of 1979 and named after its creator Stephen Bourne. The Bourne shell is designed on the basis of the Algol language, and is mainly used for automated system management. While the Bourne shell is popular for simplicity and speed, it lacks many conversational features, such as process, alias, and job control.
Shell Basic Working principle scheme
The most important system program that Linux systems provide to users is the shell command language interpreter. It is not part of the kernel, but is run as a user state outside the core. Its basic function is to interpret and execute various commands that the user has entered, and to implement the interface between the user and the Linux core. After the initial start of the system, the core establishes a process for each end user to execute the shell interpreter. Its implementation process is basically as follows:
(1) Read the command line entered by the user by the keyboard.
(2) Analyze the command, take the command name as the file name, and transform the other parameters into the form required by the system call EXECVE () internal processing.
(3) The terminal process calls fork () to establish a child process.
(4) The terminal process itself uses the system call WAIT4 () to wait for the child process to complete (not wait if it is a background command). When the child process is run, call Execve (), and the child process looks for the file (which is a file of the command interpreter) to the directory based on the file name (i.e., the command name), and calls it into memory to execute the program (explaining the command).
(5) If there is a & (Background command symbol) at the end of the command, the terminal process does not have to wait for the system call WAIT4 (), immediately sends the prompt, lets the user enter the next command, and goes to ⑴. If there is no & at the end of the command, the terminal process waits until the child process (that is, the process running the command) finishes processing, reports to the parent process (the terminal process), and when the terminal process wakes up, after making the necessary discrimination, the terminal process prompts the user to enter a new command to repeat the process.
What is a shell