Bash:使用点或“源”调用另一个脚本 - 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20094271/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:41:31  来源:igfitidea点击:

Bash: using dot or "source" calling another script - what is difference?

bash

提问by setevoy

Let's take little example:

让我们举个小例子:

$ cat source.sh
#!/bin/bash
echo "I'm file source-1"

. source-2.sh

And:

和:

$ cat source-2.sh
#!/bin/bash
echo "I'm file source-2"

Now run:

现在运行:

$ ./source.sh
I'm file source-1
I'm file source-2

If I'll change call of second file in first:

如果我首先更改第二个文件的调用:

$ cat source.sh
#!/bin/bash
echo "I'm file source-1"

source source-2.sh

It will have same affect as using dot.

它将与使用dot.

So - what is difference it this methods? Thanks.

那么 - 这种方法有什么区别?谢谢。

采纳答案by devnull

There is no difference.

没有区别。

From the manual:

手册

source

source filename

A synonym for . (see Bourne Shell Builtins).

source

source filename

A synonym for . (see Bourne Shell Builtins).

回答by chepner

The only difference is in portability. .is the POSIX-standard command for executing commands from a file; sourceis a more-readable synonym provided by bashand some other shells. bashitself, however, makes no distinction between the two.

唯一的区别是便携性。.是用于从文件执行命令的 POSIX 标准命令;source是由bash和其他一些 shell提供的更易读的同义词。bash然而,它本身并没有区分这两者。