Exec, source, and bash differences (zz)

Source: Internet
Author: User

In bash shell, source, exec, and sh can be used to execute shell scripts, but what are their differences?


SH: the parent process fork a sub-process, and shell script is executed in the sub-process.


Source: executed in the original process, not Fork sub-process


Exec: execute in the original process, but terminate the original process at the same time


Note: using export will inherit the variables in the parent process from the child process, but in turn it will not work. In the child process, no matter if the environment changes, it will not affect the parent process.

 

The following is an example.

 

[Plain]View plaincopy
  1. 1. Sh
  2. #! /Bin/bash
  3. A = B
  4. Echo "PID for 1.sh before exec/source/fork: $"
  5. Export
  6. Echo "1.sh:\ $ A is $"
  7. Case $1 in
  8. Exec)
  9. Echo "using exec ..."
  10. Exec./2.sh ;;
  11. Source)
  12. Echo "using source ..."
  13. ../2.sh ;;
  14. *)
  15. Echo "using fork by default ..."
  16. ./2.sh ;;
  17. Esac
  18. Echo "PID for 1.sh after exec/source/fork: $"
  19. Echo "1.sh:\ $ A is $"

 

[Plain]View plaincopy
  1. 2. Sh
  2. Code:
  3. #! /Bin/bash
  4. Echo "PID for 2.sh: $"
  5. Echo "2.sh get \ $ A = $ A from 1.sh"
  6. A = C
  7. Export
  8. Echo "2.sh:\ $ A is $"

Run the following command in the command line:

 

./1.sh fork

As you can see, 1. SH is executed in the parent process, 2. SH is executed in the child process. The PID of the parent process is 5344, while that of the child process is 5345. After the child process is executed, the control is returned to the parent process. At the same time, changing the value of environment variable a in a sub-process does not affect the parent process.

 

./1.sh Source

The results show that 1. SH and 2. Sh are executed in the same process, and the PID is 5367.

 

./1.sh Exec

We can see that both scripts are executed in the same process, but note that the original parent process is terminated using exec. Therefore, we can see that

 

[Plain]View plaincopy
  1. Echo "PID for 1.sh after exec/source/fork: $"
  2. Echo "1.sh:\ $ A is $"

These two commands are not executed

 

 

In this example, we can understand the differences between them.

Exec, source, and bash differences (zz)

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.