Three different methods (fork, exec, source) for calling another script in shell scripts)
Developer on Alibaba Coud: Build your first app with APIs, SDKs, and tutorials on the Alibaba Cloud. Read more ＞
Three different methods (fork, exec, source) fork (/directory/script. sh) fork is the most common, that is, directly using/directory/script in the script. sh to call the script. sh script. run a sub-sh... three different methods (fork, exec, source) fork (/directory/script. sh) fork is the most common, that is, directly using/directory/script in the script. sh 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, return parent-shell. sub-shell inherits environment variables from parent-shell. however, the environment variables in sub-shell do not bring back parent-shell exec (exec/directory/script. sh) exec is different from fork. you do not need to create a new sub-shell to execute the called script. the called script and the parent script are executed in the same shell. However, after exec is used to call a new script, 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) the difference with fork is that instead of opening a sub-shell to execute the called script, it is executed in the same shell. therefore, the variables and environment variables declared in the called script 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/bashA = B echo "PID for 1.sh before exec/source/fork: $" export Aecho "1.sh:\ $ A is $ A" case $1 in exec) echo "using exec... "Exec./2.sh; source) echo" using source... "../2.sh; *) echo" using fork by default... "./2.sh; esacecho" PID for 1.sh after exec/source/fork: $ "echo" 1.sh:\$ A is $ A "2.sh #! /Bin/bashecho "PID for 2.sh:$ $" echo "2.sh get \ $ A = $ A from 1.sh" A = Cexport Aecho" 2.sh:\ $ A is $ A "execution status: $. /1.sh PID for 1.sh before exec/source/fork: 58453641.sh: $ A is Busing fork by default... PID for 2.sh: 52429402.sh get $ A = B from 1. sh2.sh: $ A is CPID for 1.sh after exec/source/fork: 58453641.sh: $ A is B $. /1.sh execPID for 1.sh before exec/source/fork: 55626681.sh: $ A is Busing exec... PID for 2.sh: 55626682.sh get $ A = B from 1. sh2.sh: $ A is C $./1.sh source PID for 1.sh before exec/source/fork: 51568941.sh: $ A is Busing source... PID for 2.sh: 51568942.sh get $ A = B from 1. sh2.sh: $ A is CPID for 1.sh after exec/source/fork: 51568941.sh: $ A is C $
This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or
reliability of the article or any translations thereof. If you have any concerns or complaints relating to the article, please send an email, providing a detailed description of the concern or
complaint, to email@example.com. A staff member will contact you within 5 working days. Once verified, infringing content will be removed immediately.
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:
and provide relevant evidence. A staff member will contact you within 5 working days.