Shell - internal and external commands, as well as downloading the source code of the bash shell

concept

Linux commands are divided into internal (built-in) (built-in) commands and external commands. The functions of internal commands and external commands are basically the same, but there are some subtle differences.

The so-called internal and external are actually relative to Shell itself. In order to improve the operating efficiency of the system, the Linux system loads frequently used lightweight commands into the memory when the system is started for the Shell to call at any time. These commands are internal commands.

On the contrary, the heavier commands invoked by the system layer will only be loaded on the hard disk when they are invoked. These commands are external commands.

internal command

The internal command is actually a part of the shell program, which is implemented by the shell software, which contains some relatively simple linux system commands, which are recognized by the shell program and run inside the shell program, usually loaded in the linux system At runtime the shell is loaded and resides in system memory. Internal commands are an important part of the shell itself. Internal commands are embedded in the shell program and do not exist on the disk in the form of disk files alone. Internal commands are written in the bashy source code, and their execution speed is faster than external commands, because the shell does not need to create subprocesses for parsing internal commands, and they all run in the Shell process. For example: exit, history, cd, echo, fg, cd, source, export, time, etc.

external command

The external command is the utility part of the linux system, which is an independent external executable program. Because the functions of the utility program are usually relatively powerful, the amount of programs contained in it will be large, and it will not be downloaded together with the system when the system is loaded. Loaded into memory, but called into memory only when needed. Usually the entity of the external command is not included in the shell, but its command execution process is controlled by the shell program. When an external command is called, the essence is to call another program. First, the Shell will create a subprocess, and then run the program in the subprocess. The Shell program manages the path search, loading and storing of external commands, and controls the execution of commands. External commands are additionally installed outside of bash, usually placed in /bin, /usr/bin, /sbin, /usr/sbin...etc. The storage path of external commands can be viewed through the "echo $PATH" command. Common external commands such as: /bin/ls, vi, tee, tar, etc.

Why divide internal commands and external commands

Internal commands are actually a part of the SHELL program, which contains some relatively concise and frequently used commands in daily life. These commands are usually loaded into the memory when the system starts, and are resident in the memory. They are recognized by the SHELL program and run inside the SHELL program. There is only one reason for this: to maximize execution efficiency and improve system performance. The external command is usually a software function of the system. This part of the program function is usually relatively powerful, but the amount of the program included is also large, so it is not loaded together with the system startup, and is only read from the hard disk into the memory when the user needs it.

Execution process and sequence of internal and external commands

Because the internal command is built-in by SHELL, it can be used directly when calling the command without searching for environment variables. However, the external command is quite different, because if the external command is to be used by the user, it needs to be called by the SHELL program, so there is one more The problem of layer execution path is the environment variable we often talk about.

An external command is a command executed by a copy of the Shell (new process). The basic process is as follows:

* a. Create a new process. This process is a copy of the Shell.

* b. In the new process, look for the specified command in the directories listed in the PATH variable.

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin: is a typical default value for the PATH variable. When the command name contains a slash (/) symbol, the path search step will be skipped.

* c. In the new process, replace the executing Shell program with the new program found and execute it.

* d. After the program is completed, the original shell will then read the next command from the terminal and execute the next command in the script.

Normally, the Bash built-in commands in the script will not fork a child process when they are run. But external or filter commands in scripts usually fork a child process. A builtin command usually has the same name as a system command (external command), but Bash reimplements these commands internally. For example, Bash's echo command is not the same as /bin/echo, although their behavior is the same in most cases.

Similarly, if the external command corresponding to our internal command is moved to a directory other than the system environment variable or deleted. The command can actually still be executed, because the search order is internal command -> external command.

When the shell command interpreter executes a command, it first tries to execute it according to the internal command. If the command to be executed is not an internal command, it searches for the directory where the corresponding execution file is located according to the external command, and executes it. When the command to be executed is not an internal command (such as ls), if there are two ls commands in different directories (such as /usr/local/bin/ls and /bin/ls), the shell command interpreter will be based on PATH Which directory is queried first, the commands in that directory will be executed first.

Common Internal Commands

There are about 60 built-in commands in SHELL, and you can view all the built-in commands through the built-in enable command.

$ enable

enable .

enable :

enable [

enable alias

enable bg

enable bind

enable break

enable builtin

enable caller

enable cd

enable command

enable compgen

enable complete

enable compopt

enable continue

enable declare

enable dirs

enable disown

enable echo

enable enable

enable eval

enable exec

enable exit

enable export

enable false

enable fc

enable fg

enable getopts

enable hash

enable help

enable history

enable jobs

enable kill

enable let

enable local

enable logout

enable mapfile

enable popd

enable printf

enable pushd

enable pwd

enable read

enable readarray

enable readonly

enable return

enable set

enable shift

enable shopt

enable source

enable suspend

enable test

enable times

enable trap

enable true

enable type

enable typeset

enable ulimit

enable umask

enable unalias

enable unset

enable wait

Disable (turn off) builtin commands

enable -n cd means to disable the built-in function of the command cd in SHELL.

 View internal command source code

View the shell currently used by the system:

$ env | grep SHELL

SHELL=/bin/bash

GNOME_SHELL_SESSION_MODE=ubuntu

The current shell uses bash, check the version of bash:

$ bash --version

GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)

Copyright (C) 2019 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law.

The bash package can be found at Software- GNU Project - Free Software Foundation . Click to enter the page of bash, which contains the download link of the source code.

bash source code path: Index of /gnu/bash , download the specified version from this path. The source code is written in C language.

Note:

1. Use the type command, which can be used to display the type of executable command to distinguish whether it is a shell internal command or an external command.

reference:

1, Linux Shell internal commands and external commands

Linux Shell internal commands and external commands - Programmer Sought

Guess you like

Origin blog.csdn.net/guoqx/article/details/131041505