/*****************************
* Author : Samson
* Date : 12/05/2015
* Test platform:
* GNU bash, 4.3.11
* Debian GNU/Linux 8
* ***************************/
目前来说有三种方法:
1. 脚本绝对路径
这个方式是最普通的,底层调用的是fork实现,运行的时候开一个子shell执行调用的脚本,子shell执行的时候,父shell还在
子shell执行完毕后返回父shell,子shell从父shell继承环境变量,但是子shell中的环境变量不会带回父shell中。
2. 句号 + 脚本绝对路径
底层调用的是source实现的,他于fork的区别是不新开一个子shell来执行被调用的脚本,而是在同一个shell中执行,所以被调用脚本中申明的变量和环境变量,都可以在主脚本中得到和使用。
3. exec + 脚本绝对路径
exec与fork不同,不需要新开一个子shell来执行被调用的脚本,而是在同一个shell中执行,但是使用exec调用一个新脚本以后,父脚本exec以后的内容就不会再执行了。
下面是一个脚本的例子:
父脚本(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"
```
#!/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"
PID for 1.sh before exec/source/fork:10124
1.sh: $A is B
using fork by default…
PID for 2.sh: 10125
2.sh get $A=B from 1.sh
2.sh: $A is C
PID for 1.sh after exec/source/fork:10124
1.sh: $A is B
PID for 1.sh before exec/source/fork:10143
1.sh: $A is B
using source…
PID for 2.sh: 10143
2.sh get $A=B from 1.sh
2.sh: $A is C
PID for 1.sh after exec/source/fork:10143
1.sh: $A is C
~/ $ 1.sh exec
1.sh:未找到命令
~/ $ ./1.sh exec
PID for 1.sh before exec/source/fork:10140
1.sh: $A is B
using exec…
PID for 2.sh: 10140
2.sh get $A=B from 1.sh
2.sh: $A is C
Unless command is omitted, the shell process is replaced with the specified program (which must be a real program, not a shell builtin or function). Any redirections on the exec command are marked as permanent, so that they are not undone when the exec command finishes.