美文网首页
shell source 和 xxx.sh 和 sh xxx.s

shell source 和 xxx.sh 和 sh xxx.s

作者: 耳机在哪里 | 来源:发表于2019-01-24 20:30 被阅读0次

source xxx.sh: 直接在当前 shell 中执行该脚本。
sh xxx.sh: 在当前 shell 中新建一个子 shell 执行该脚本。
xxx.sh: 有执行权限时,和 sh xxx.sh 没有区别。

比如:
脚本里遇到异常情况,想要退出时,会写 exit 1
如果使用 source xxx.sh 遇到 exit 会直接退出控制台;使用 sh xxx.sh 就能退出文件回到控制台。

就算写了 export,变量的值也会随着子 shell 的销毁而销毁

比如:
脚本里写了 export
如果使用 source xxx.sh ,脚本中定义的变量值能 echo;使用 sh xxx.sh 变量值就会丢失。

相关文章

网友评论

      本文标题:shell source 和 xxx.sh 和 sh xxx.s

      本文链接:https://www.haomeiwen.com/subject/mvwujqtx.html