有很多方式可以执行脚本,用于读取并执行test.sh中的命令,区别如下:
|
source test.sh |
执行过程不另开进程,脚本文件中设定的变量在当前shell中可以看到 |
|
. test.sh | |
|
./test.bsh |
在当前进程另开子进程来执行脚本命令,脚本文件中设定的变量在当前shell中不能看到 |
|
sh test.sh |
例如:
liuwanpeng@ubuntu:~$ cat test.sh
echo "pid in test.sh" $$
var=123
liuwanpeng@ubuntu:~$ echo $$8574
liuwanpeng@ubuntu:~$ unset var;source test.sh ;echo $var
pid in test.sh
8574
liuwanpeng@ubuntu:~$ unset var;. test.sh ;echo $var
pid in test.sh 8574
123
liuwanpeng@ubuntu:~$ unset var;./test.sh ;echo $var
pid in test.sh 8670
iuwanpeng@ubuntu:~$ unset var;sh test.sh ;echo $var
pid in test.sh 8671
本文介绍了执行Shell脚本的几种常见方式及其区别,包括使用source、. (点操作符)、直接调用脚本文件及通过sh命令执行。通过示例展示了不同执行方式下变量可见性的差异。
3万+

被折叠的 条评论
为什么被折叠?



