在调用另一个脚本时使用点或“ source”——有什么区别?

让我们举个小例子:

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


. source-2.sh

还有:

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

快跑:

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

如果我将第二个文件的调用改为:

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


source source-2.sh

它将具有与使用 dot相同的效果。

这些方法有什么不同?

32263 次浏览

There is no difference.

From the manual:

source

source filename


A synonym for . (see Bourne Shell Builtins).

The only difference is in portability.

. is the POSIX-standard command for executing commands from a file; source is a more-readable synonym provided by Bash and some other shells. Bash itself, however, makes no distinction between the two.