当前位置:编程学习 > 网站相关 >>

shell 脚本各种执行方式(source ./*.sh, . ./*.sh, ./*.sh)的区别

结论一: ./*.sh的执行方式等价于sh ./*.sh或者bash ./*.sh,此三种执行脚本的方式都是重新启动一个子shell,在子shell中执行此脚本。
结论二: .source ./*.sh和 . ./*.sh的执行方式是等价的,即两种执行方式都是在当前shell进程中执行此脚本,而不是重新启动一个shell 而在子shell进程中执行此脚本。
 
验证依据:没有被export导出的变量(即非环境变量)是不能被子shell继承的
验证结果:
[html]  
<pre name="code" class="html">[root@localhost ~]#name=dangxu       //定义一般变量</pre>[root@localhost ~]# echo ${name}<br>  
dangxu<br>  
[root@localhost ~]# cat test.sh //验证脚本,实例化标题中的./*.sh#!/bin/shecho ${name}<br>  
[root@localhost ~]# ls -l test.sh //验证脚本可执行<br>  
-rwxr-xr-x 1 root root 23 Feb  6 11:09 test.sh<br>  
[root@localhost ~]# ./test.sh //以下三个命令证明了结论一<br>  
<br>  
<pre name="code" class="html">[root@localhost ~]# sh ./test.sh  
  
[root@localhost ~]# bash ./test.sh  
</pre>[root@localhost ~]# . ./test.sh //以下两个命令证明了结论二<br>  
dangxu<br>  www.zzzyk.com
[root@localhost ~]# source ./test.sh<br>  
dangxu<br>  
[root@localhost ~]#  
<pre></pre>  
<p></p>  
<pre></pre>  
<br>  
<br>  
<p></p>  
补充:综合编程 , 其他综合 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,