Three different ways to invoke another script in a shell script (fork, exec, source)

Source: Internet
Author: User

Fork (/directory/script.sh): If the shell contains execution commands, then the subcommand does not affect the parent's commands, and then executes the parent command after the subcommands have finished executing. The children's environment variables do not affect the parent level.
Fork is the most common, is directly in the script with/directory/script.sh to call script.sh this script.
run the time to open a Sub-shell execute the call script, Sub-shell execution, Parent-shell is still in.
Sub-shell returns Parent-shell when execution is complete. Sub-shell inherits environment variables from Parent-shell. But the environment variables in Sub-shell are not brought back to Parent-shell

exec (exec/directory/script.sh): After executing a child's command, the parent command is no longer executed.
Unlike fork, exec does not need to open a new Sub-shell to execute the called script. The invoked script executes within the same shell as the parent script. However, after calling a new script with exec, the content after the Exec line in the parent script is no longer executed. This is the difference between exec and source.

third, Source (source/directory/script.sh): Executes the child command and resumes execution of the parent command, while the environment variables set by the child affect the environment variables of the parent.
the difference from fork is that it does not open a new Sub-shell to execute the called script, but rather executes it in the same shell. The variables and environment variables declared in the invoked script can be obtained and used in the main script.

The following two scripts can be used to realize the different three kinds of calling methods :
Cat 1.sh:

#!/bin/basha=b echo "PID for 1.sh before exec/source/fork:$$" Export Aecho "1.sh: \ $A are $A" case $ 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"

Cat 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"

Results:

[[email protected] ~]#./1.sh PID for 1.sh befor exec/source/fork:10761.sh: $A are busing fork by default ... PID for 2.sh:10772.sh get $A =b from 1.sh2.sh: $A are CPID for 1.sh after exec/source/fork:10761.sh: $A is B[[email protecte D] ~]#./1.sh execpid for 1.sh befor exec/source/fork:10781.sh: $A is busing exec ... PID for 2.sh:10782.sh get $A =b from 1.sh2.sh: $A are c[[email protected] ~]#./1.sh sourcepid for 1.sh befor exec/source/f Ork:10791.sh: $A is busing source ... PID for 2.sh:10792.sh get $A =b from 1.sh2.sh: $A are CPID for 1.sh after exec/source/fork:10791.sh: $A is C[[email protecte D] ~]#

Thus:

Fork () 1.sh and 2.sh each is a different process, the child process inherits the environment variables of the parent process, creates a new process, and then returns to the parent process after the child process has finished. This is one of the most commonly used shells in the shell.

EXEC () 1.sh and 2.sh both print the same process number, is the same process, the child process creation is the replacement of the parent process, the end of the child process, the process is retired, did not print the original belongs to 1.sh after part. Verify that exec LS can be executed in the shell so that it exits the login.
Source () 1.sh and 2.sh are the same process, the subroutine's commands are completed following the execution of the parent program's commands, of course, the subroutine changes the environment variables that affect the parent program because of the same process.

Three different ways to invoke another script in a shell script (fork, exec, source)

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.