分享

shell 脚本各种执行方式(source ./*.sh, . ./*.sh, ./*.s...

 lifei_szdz 2013-06-21

原文出处:http://blog.csdn.net/dance_rise/article/details/8573560

结论一: ./*.sh的执行方式等价于sh ./*.sh或者bash ./*.sh,此三种执行脚本的方式都是重新启动一个子shell,在子shell中执行此脚本。

结论二: .source ./*.sh和 . ./*.sh的执行方式是等价的,即两种执行方式都是在当前shell进程中执行此脚本,而不是重新启动一个shell 而在子shell进程中执行此脚本。

验证依据:没有被export导出的变量(即非环境变量)是不能被子shell继承的

验证结果:

  1. [root@localhost ~]#name=dangxu       //定义一般变量  
  2. [root@localhost ~]# echo ${name}  
  3. dangxu  
  4. [root@localhost ~]# cat test.sh      //验证脚本,实例化标题中的./*.sh  
  5. #!/bin/sh  
  6. echo ${name}  
  7. [root@localhost ~]# ls -l test.sh    //验证脚本可执行  
  8. -rwxr-xr-x 1 root root 23 Feb  6 11:09 test.sh  
  9. [root@localhost ~]# ./test.sh        //以下三个命令证明了结论一  
  10.   
  11. [root@localhost ~]# sh ./test.sh  
  12.   
  13. [root@localhost ~]# bash ./test.sh  
  14.   
  15. [root@localhost ~]# . ./test.sh     //以下两个命令证明了结论二  
  16. dangxu  
  17. [root@localhost ~]# source ./test.sh  
  18. dangxu  
  19. [root@localhost ~]#  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多