From: http://mindream.wang.blog.163.com/blog/static/2325122220084624318692/
Fork (/directory/script. Sh)
Fork is the most common. It uses/directory/script. Sh directly in the script to call the script. Sh script.
Run a sub-shell script to execute the call. When Sub-shell is executed, parent-shell is still running.
After sub-shell is executed, the system returns parent-shell. Sub-shell to inherit environment variables from parent-shell. However, the environment variables in sub-shell are not returned.
Parent-shell
Exec (exec/directory/script. Sh)
Unlike fork, exec does not require a new sub-shell to execute the called script. The called script and the parent script are executed in the same shell. However
After a new script is called using exec, the content after the exec line in the parent script will not be executed again. This is the difference between exec and source.
Source (Source/directory/script. Sh can also be clicked, that is.
/Directory/script. Sh)
The difference with fork is that you do not open a sub-shell to execute the called script, but execute it in the same shell. Therefore, the declared
Both variables and environment variables can be obtained and used in the main script.
You can use the following two scripts to understand the differences between the three call methods:
1. Sh
#! /Bin/bash
A = B
Echo "PID for 1.sh before exec/source/fork: $"
Export
Echo "1.sh:\ $ A is $"
Case $1 in
Exec)
Echo "using exec... "
Exec./2.sh ;;
Source)
Echo "using source... "
../2.sh ;;
*)
Echo "using fork by default... "
./2.sh ;;
Esac
Echo "PID for 1.sh after exec/source/fork: $"
Echo "1.sh:\ $ A is $"
2. Sh
#! /Bin/bash
Echo "PID for 2.sh:$ $"
Echo "2.sh get \ $ A = $ A from 1.sh"
A = C
Export
Echo "2.sh:\ $ A is $"
Execution status:
$./1.sh
PID for 1.sh before exec/source/fork: 5845364
1. sh: $ A is B
Using fork by default...
PID for 2.sh: 5242940
2. Sh get $ A = B from 1.sh
2. sh: $ A is C
PID for 1.sh after exec/source/fork: 5845364
1. sh: $ A is B
$./1.sh Exec
PID for 1.sh before exec/source/fork: 5562668
1. sh: $ A is B
Using exec...
PID for 2.sh: 5562668
2. Sh get $ A = B from 1.sh
2. sh: $ A is C
$./1.sh Source
PID for 1.sh before exec/source/fork: 5156894
1. sh: $ A is B
Using source...
PID for 2.sh: 5156894
2. Sh get $ A = B from 1.sh
2. sh: $ A is C
PID for 1.sh after exec/source/fork: 5156894
1. sh: $ A is C
$