Fork, source and exec differences in running scripts

Source: Internet
Author: User

Fork, source and exec differences in running scripts
When the fork method is used to run the script, the shell (parent process) generates a child process to execute the script. When the child process ends, the parent process is returned, however, the environment of the parent process will not change because of the change of the child process. when the source method is used to run a script, the script is executed in the current process, rather than a child process. because all execution results are completed in the current process, if the script environment changes, the current process environment will also change. when exec is used to run a script in exec mode, it is the same as source, so that the script is executed in the current process, The remaining part of the original code will be terminated. Similarly, the environment in the process changes with the script. Conclusion: Generally, If we execute the code, the fork is used by default. You can run the pstree command to check the relationship between parent and child processes. As shown above, if you want the parent process to get the environment variable of the child process, it is the script [SQL] # in source Mode 1. sh! /Bin/bash A = B echo "PID for before exec/source/fork: $" export A echo "\ $ A is $ A" case $1 in exec) echo "using exec... "Exec./; source) echo" using source... "../; *) echo" using fork by default... ". /; esac echo "PID for after exec/source/fork: $" echo "\ $ A is $ A" 2. sh script [SQL] #! /Bin/bash echo "PID for$ $" echo " get \ $ A = $ A from" A = C export A echo"\ $ A is $ A "3. experiment 3.1 fork [SQL] PID for before exec/source/fork: 25992 $ A is B using fork by default... PID for 25993 get $ A = B from $ A is C PID for after exec/source/fork: 25992 $ A is B 3.2 source [SQL] PID for before exec/source/fork: 25994 $ A is B using source... PID for 25994 get $ A = B from $ A is C PID for after exec/source/fork: 25994 $ A is C note: all three PIDs are 25994, and the environmental variables of the subprogram are included. 3.3 exec [SQL] PID for before exec/source/fork: 25997 $ A is B using exec... PID for 25997 get $ A = B from $ A is C note: The rest of the main program is not executed.

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: 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.