Shell is a command-line interpreter
For general users, we cannot use the operating system directly (kernel). Instead, it communicates with kernel through kernel's "shell" program, the so-called shell.
Why can't I use kernel directly?
From a technical point of view, the simplest definition of the shell: command-line interpreter interpreter
Mainly includes:
A. Translate user commands to core (kernel) processing.
B. At the same time, the core processing results are translated to the user.
For the Windows GUI, we operate windows not directly in the Windows kernel, but through the graphical interface, click to complete our operations (such as entering the D-drive operation, we usually double-click the D-drive letter. or run an application). The shell has the same effect on Linux, mainly parsing our instructions and parsing instructions to the Linux kernel. Feedback results are applied through the kernel and parsed to the user through the shell.
The general role of the shell
The
UNIX shell program interprets the user's commands, and the shell scripts are interpreted rather than compiled. In addition to transferring commands to the kernel, the primary task of the shell is to provide a user environment that can be individually configured to use the shell resource profile. An important part of the
Shell task is the search command. Bash is done in the next step: check to see if the command contains slashes. If not, first check to see if the list of functions contains a command we are looking for. If the command is not a function, check it in the list of built-in commands. The shell built-in command refers to commands in the bash (or other versions) toolset. There is usually a system command with the same name, such as the echo command in bash and the/bin/echo are two different commands, although they are roughly similar in their behavior. When you type a command in bash, the system will first see if he is a built-in command, and if not, see if it is a system command or a third-party tool. So typing the echo command in bash actually executes the Bash command in the Bash toolset, which is the built-in command instead of/bin/echo the system command. Note: The type command in Linux if the command is neither a function nor a built-in command, scan the list of directories listed in PATH to find it. Bash uses a hashtable (in-memory data storage area) to memorize the full path of the executable file, which prevents an extended search of path. If the search does not succeed, bash prints an error message and returns exit status 127. If the search succeeds or the command contains a slash, the shell executes the command in a separate execution environment. If the execution fails because the file is not executable or is not a directory, it is assumed to be a shell script. If the command starts asynchronously, the shell waits for the command to complete and collects its exit status.
How the shell runs the program. The shell creates a new process with fork, runs the user-specified program in the new process with the EXECV function cluster, and finally the shell waits for the new process to end with the wait command. The wait system call also takes an exit state from the kernel or a signal sequence to tell the child how to end the process. The shell outputs a prompt, waits for you to enter a line of commands, and then executes the command. To abort this command, the shell then outputs a prompt waiting for the next input command line.
Take the HELLO.C procedure as an example. First, the shell program executes its instructions and waits for us to enter the command. When we enter the string "./hello" on the keyboard, the shell program reads the character to the register and then stores it in memory. When we hit enter on the keyboard, the shell knew that we had finished typing the command. The shell then executes a series of instructions that copy the code and data from the Hello destination file from disk to main memory to load the hello file. The data includes the string "hello,world\n" that will eventually be output. The shell loads and executes the Hello program, and then waits for the program to terminate, and the Hello program outputs his message on the screen and then terminates. The shell then outputs a prompt that waits for the next input command line.
How the Linux shell commands work