Linux command operation principle shell and bash

foreword

In the process of learning the operating system, we often execute some Linux commands in our own shell, so what happens when we enter a command like ls -a?

In other words, after we enter ls -a in the shell and press Enter, how is this command executed at the bottom of Linux? Let's analyze it in detail below!


What is shell, what is bash?

Write picture description here

The shell is an interpreter (also a process) running in the user mode , which is equivalent to the "user interface" of the operating system, and is an intermediate bridge between the user and the kernel communication . It is like the shell of the kernel . Our users interact with the shell to achieve operation of the system;

And bash (/bin/bash) is the default shell in linux. It is a continuously running process that starts after the user connects to the OS;


ls -l execution process

To answer the questions posed in the preface, what happens when you enter an ls -l in the shell command box?

As shown below:
insert image description here

When a user connects to the system, a shell (bash) process belonging to him will be created to form a terminal interface, waiting for the user to input commands to operate;

When we press ls -l:

  1. The main(argc, argv) function of the shell will read the strings ls and -l in the form of command line parameters , and internally parse out the command name ls and parameter l;
  2. Checks whether the entered command is a full command or an alias and replaces it with the original command name;

(For example, alias rm='rm -i'; give the rm -i command an alias rm, and remind you to enter Y/N when deleting something to prevent users from accidentally deleting! These mechanisms do not conflict with the above command processing flow!)

  1. Further check whether the original command is an internal command (program) or an external command (program);

(type+command can check whether it is an external or internal command)

  1. Find the location of this command (program), and run this command (program) in a certain way in combination with the corresponding options, and return the result;


In fact, various commands are essentially independent programs:

Internal commands (programs) are stored in the memory together with the initial creation of the shell process, which is equivalent to some functions in the shell process and run directly without the need for file system IO into the memory, and are not affected by environment variables, so the execution efficiency relatively high!

ls cd etc. are internal commands; vim cat etc. are external commands

External commands (programs) exist in certain PATH environment variable paths. The shell needs to find the program path by path, and enter the memory through the IO of the file system inode node, and use the shell to fork out of the child process to replace the exec program . Afterwards, the shell will recycle, so the execution efficiency is slow;

(The reason for creating a child process and replacing the exec process is for the security of the shell. In case an illegal third-party external command is designed by a hacker, it cannot be allowed to affect the shell interpreter)

If the typed command is neither an internal command nor an external PATH path , a command not found prompt message will be displayed;;

Guess you like

Origin blog.csdn.net/wtl666_6/article/details/129740934
Recommended