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

  • Reprinted from: http://blog.chinaunix.net/uid-22548820-id-3181798.html
  • fork ( /directory/script.sh) : If the shell contains an execution command, the subcommand does not affect the parent's command, and the parent command is executed after the subcommand is executed. The environment variables of the child do not affect the parent.

Fork is the most common, which is to use /directory/script.sh to call script.sh directly in the script.

When running, open a sub-shell to execute the called script. When the sub-shell is executed, the parent-shell is still there.

Sub-shell returns to parent-shell after execution. Sub-shell inherits environment variables from parent-shell. But environment variables in sub-shell will not be brought back to parent-shell

  • exec (exec /directory/script.sh): After executing the child's command, the parent's command is no longer executed.

Unlike fork, exec does not need to open a new sub-shell to execute the called script. The called script is executed in the same shell as the parent script. But after calling a new script with exec, the content after the exec line in the parent script will not be executed. This is the difference between exec and source

  • source (source /directory/script.sh): After executing the child command, continue to execute the parent command, and the environment variables set by the child will affect the parent's environment variables.

The difference from fork is that it does not open a new sub-shell to execute the called script, but executes it in the same shell. Therefore, the variables and environment variables declared in the called script can be obtained and used in the main script .

 

You can experience the difference between the three calling methods through the following two scripts:

1.sh 

#!/bin/bash
A=B 
echo "PID for 1.sh before exec/source/fork:$$"
export A
echo "1.sh: \$A is $A"
case $1 in
        exec)
                echo "using exec…"
                exec ./2.sh ;;
        source)
                echo "using source…"
                . ./2.sh ;;
        *)
                echo "using fork by default…"
                ./2.sh ;;
esac
echo "PID for 1.sh after exec/source/fork:$$"
echo "1.sh: \$A is $A"

2.sh 

#!/bin/bash
echo "PID for 2.sh: $$"
echo "2.sh get \$A=$A from 1.sh"
A=C
export A
echo "2.sh: \$A is $A"

 

 

执行情况:

$ ./1.sh     
PID for 1.sh before exec/source/fork:5845364
1.sh: $A is B
using fork by default…
PID for 2.sh: 5242940
2.sh get $A=B from 1.sh
2.sh: $A is C
PID for 1.sh after exec/source/fork:5845364
1.sh: $A is B
$ ./1.sh exec
PID for 1.sh before exec/source/fork:5562668
1.sh: $A is B
using exec…
PID for 2.sh: 5562668
2.sh get $A=B from 1.sh
2.sh: $A is C
$ ./1.sh source 
PID for 1.sh before exec/source/fork:5156894
1.sh: $A is B
using source…
PID for 2.sh: 5156894
2.sh get $A=B from 1.sh
2.sh: $A is C
PID for 1.sh after exec/source/fork:5156894
1.sh: $A is C
$

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326484337&siteId=291194637