Three different methods (fork, exec, source) for calling another script in shell scripts)

Source: Internet
Author: User
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 $
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.