The Shell function is to explain the execution of the user's command, the user enters a command, the shell explains the execution of this article, this method is called interactive, but there is another way to execute the command is called batch mode, the user wrote a shell script, the shell can be executed at once these commands.
Here's an example:
The first method of execution: Chmode+x script.sh
Execution process:
The shell will fork a child process and call exec to execute./script.sh This program, the EXEC system call will replace the process code snippet with the./script.sh Program code snippet, exec has a mechanism, if you want to execute a text file, And the first line shebang specifies the interpreter, replaces the current process with the code snippet of the interpreter program, and executes from the _start of the interpreter, and the text file is passed to the interpreter as a command-line argument
The second mode of execution:
Shell execution Process:
1. Interactive shell (bash) fork/exec a child shell (SH) is used to execute the script, and the parent process bash waits for the child process sh to terminate.
2. SH read the CD in the script. command, call the appropriate function to execute the built- in command ( do not create the child process, run the parent process ), and change the current working directory to the top level directory .
3. SH reads the LS command in the script, fork/exec This program, lists the files under the current working directory, SH waits for LS to terminate.
4. After the LS terminates, SH continues to execute, reading to the end of the script file, SH terminates.
5. When SH terminates, bash resumes execution and the print prompt waits for user input.
The effect of the two methods of executing the shell script is the same, CD: Command changes the PWD of the child shell without affecting the interactive shell.
source./script.sh or. ./script.sh command:
The source or. command is the shell's built-in command, which does not create a child shell, but rather executes the commands in the script line-by-row under the interactive shell.
Shell Execution Script