Linux built-in command (built-in) and external command __linux

Source: Internet
Author: User

Original website: http://blog.csdn.net/taohuaxinmu123/article/details/10845001

Linux commands have internal commands (built-in commands) and external commands, and internal commands are essentially the same as external commands, but there are subtle differences.

internal commands are actually part of the shell program, which contains some simpler Linux system commands that are identified by the shell program and run inside the shell program. The shell is typically loaded and resides in system memory when the Linux system loads the runtime. The internal command is written in the Bashy source code, and it executes faster than the external command because parsing the internal command shell does not require the creation of child processes. For example: Exit,history,cd,echo and so on.

external Commands are part of the utility in a Linux system, because the utility is usually powerful, so it contains a large number of programs that are not loaded into memory with the system when it is loaded, but are called into memory when it is needed. Typically, an entity of an external command is not included in the shell, but its command execution process is controlled by the shell. The shell program manages the path lookup, loading and storage of the external command execution, and controls the execution of the command. External commands are installed outside of bash, usually in/bin,/usr/bin,/sbin,/usr/sbin ... Wait a minute. You can view the storage path of an external command through the echo $PATH command, such as LS, VI, and so on.

Use the type command to distinguish between internal commands and external commands

[Root@localhost account]# Type cd

CD is a shell builtin

The PWD command is used to display the current working directory and is one of the most commonly used commands on Linux systems. When you are unsure of the current position, you can use the PWD command to determine the exact location of the directory within the file system. When it comes to PWD directives, you have to mention three environment variables: PATH, Oldpwd, PWD.

Path: A variable that executes the path of the file; "Echo $PATH" (plus $ for path preceded by a variable)

OLDPWD: Indicates the previous working directory;

PWD: Represents the current working directory.

Problem: When you enter "man pwd" you can view the PWD Help document, and the input "pwd--help" prompts error BASH:PWD:--Invalid option; If you enter/bin/pwd--help, you can display the PWD help document normally.

Look up the reason from the Internet and through the integration know, this is mainly because of internal command and external command difference, PWD is internal command, and/bin/pwd is external command. When displaying the current directory,/BIN/PWD can display the full file path of the current working directory, more accurately. When multiple people share the same Linux machine, it is often found that the current directory is deleted by someone else, and the PWD command still displays that directory.

Follow-up test: Switch to root permissions, the/bin directory of the execution of the file LS to move to the informal directory, Mv/bin/ls/root (#mv为移动, files can be moved between different directories), and then no matter which directory to enter any of the following LS-related instructions, Cannot execute LS successfully (because the/root directory is not in the directory specified by path, the external command LS is unable to find the execution file ls through the path path) unless it is/root/ls.

If you want root to execute the LS at/root under any directory, you can add/root to the PATH, which path= "$PATH":/root. If there are two LS instructions in different directories, such as/usr/local/bin/ls and/bin/ls, then when I release the LS command, according to which directory in the path is queried first, then the instructions in that directory are executed first .

Even if you move the execution file pwd in the/bin directory in the same way, you can still enter the PWD command execution, because it is still calling the internal command PWD directive.

Therefore, when executing a command, the shell command interpreter should execute the internal instruction first, and the instruction to execute is not an internal instruction, it should be an external instruction of the call.

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.