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.



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

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

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

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

EXEC mode

1:  exec./

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

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


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

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

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

~ $./ Source

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

~ $./ Exec

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

Note: When exec is used for execution, the last two sentences in 1. Sh are 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.