Summary of fork, source, and exec in Shell (including environment variables)

Source: Internet
Author: User

Summary: three methods of executing shell scripts, fork, source and exec, are summarized.

 

Prerequisites

1. any program we run is a child process generated by the parent process. After the child process is completed, it is returned to the parent process. This image is called fork in Linux. When a child process is generated, it will obtain a certain amount of resource allocation from the parent process and (more importantly) inherit the environment of the parent process ﹗
2. Shell variables can be roughly divided into three types:
Internal variables: provided by the system. They do not need to be defined and cannot be modified, for example, $ #, $ ?, $ *, $0, etc.
Environment variable: provided by the system. It can be modified without definition and used by the current process and its sub-processes, such as path, PWD, Shell, etc.
User variable (local variable): User-Defined, changeable, used in the current process, such as Var = 123
Differences from other languages: Non-type, that is, you do not need to specify a variable as a number or string.
3. environment variables: environment variables can only be inherited from the parent process to the child process in one way. In other words, changing the environment in a child process does not affect the environment of the parent process.
4. shell script: Write the multi-line commands you input after shell prompt in sequence to a file.

 

Fork, source and exec execute shell scripts

Fork Mode
This is also a common method. Generally, you can directly input the script file path in shell. In this way, the current process creates a sub-process.

1:  ./mytest.sh


Source Method
The usage is as follows (source and "." are equivalent ):

1:  source ./mytest.sh
2: or
3:  ../mytest.sh

The source method is executed in the Current Shell environment instead of creating sub-processes.

EXEC mode

1:  exec./mytest.sh

This method is characterized by not creating a sub-process, but terminating the current shell execution (in fact, I think this understanding may be more accurate: when exec is used, a subthread is created in the current process space, and the execution of the current thread is terminated. After the execution of the new thread is completed, both threads are terminated, that is, the current shell process is terminated)

Test 1
Create test1.sh with the following content:

1:  #!/bin/sh
2:  cd~/bin
3:  pwd

Three methods are used for execution. Both the source and exec Methods Apply the directory changes to the current environment. The difference is that the shell environment cannot be used after the exec method is executed, A new shell environment (process) is automatically restarted)

Test 2
A more detailed test
Script 1.sh

 1:  #!/bin/sh
 2:  A=B
 3:  echo"PID for 1.sh before exec/source/fork:$$"
 4:  exportA
 5:  echo"1.sh: /$A is $A"
 6:  case$1in
 7:  exec)
 8:  echo"using exec..."
 9:  exec./2.sh;;
10:  source)
11:  echo"using source..."
12:  ../2.sh;;
13:  *)
14:  echo"using fork by default..."
15:  ./2.sh;;
16:  esac
17:  echo"PID for 1.sh after exec/source/fork:$$"
18:  echo"1.sh: /$A is $A"

Script 2.sh

1:  #!/bin/sh
2:  echo"PID for 2.sh: $$"
3:  echo"2.sh get /$A=$A from 1.sh"
4:  A=C
5:  exportA
6:  echo"2.sh: /$A is $A"

Execute the 1. Sh script in three ways. The result is as follows:
~ $./1.sh fork

1:  PID for 1.sh before exec/source/fork:531
2:  1.sh: $A is B
3:  using fork by default...
4:  PID for 2.sh:532
5:  2.sh get $A=B from 1.sh
6:  2.sh: $A is C
7:  PID for 1.sh after exec/source/fork:531
8:  1.sh: $A is B

~ $./1.sh Source

1:  PID for 1.sh before exec/source/fork:533
2:  1.sh: $A is B
3:  using source...
4:  PID for 2.sh:533
5:  2.sh get $A=B from 1.sh
6:  2.sh: $A is C
7:  PID for 1.sh after exec/source/fork:533
8:  1.sh: $A is C

~ $./1.sh Exec

1:  PID for 1.sh before exec/source/fork:537
2:  1.sh: $A is B
3:  using exec...
4:  PID for 2.sh:537
5:  2.sh get $A=B from 1.sh
6:  2.sh: $A is C

Note: When exec is used for execution, the last two sentences in 1. Sh are not executed.

References:

Http://gaoyj1973.itpub.net/post/11309/498996
Http://hi.baidu.com/cn_linux/blog/item/923cad8bf46cac7e9e2fb49d.html
Http://blog.chinaunix.net/u2/63996/showart_573729.html
Http://hi.baidu.com/252568175/blog/item/f3489918d585a80b34fa4143.html

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.