Linux “源脚本.sh”和“./script.sh”有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4779756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between "source script.sh" and "./script.sh"?
提问by Arovit
What is the difference between source <script>
and ./<script>
?
source <script>
和 和有./<script>
什么区别?
采纳答案by Amber
source script.sh
runs the script within the current process, thus all variable assignments are preserved as variables even after the script finishes (and don't have to be explicitly export
'd).
source script.sh
在当前进程中运行脚本,因此即使在脚本完成后,所有变量分配都保留为变量(并且不必显式export
'd)。
./script.sh
just runs the script in a subprocess, and any variables which are assigned disappear after the script is done.
./script.sh
只需在子进程中运行脚本,脚本完成后分配的任何变量都会消失。
回答by klang
source script will change your current environment, ./script will not.
source script 会改变你当前的环境,./script 不会。
(EDIT: script has to be executable to use ./)
(编辑:脚本必须是可执行的才能使用 ./)