我以为这source
是 bash 中的同义词.
。但是,似乎在.profile
文件中source
不起作用。这个 YouTube 视频演示了当source
在 中使用~/.profile
来获取文件时foo
,该文件中定义的变量不会导出到后续 shell。但是,如果使用 来获取文件.
,则变量会按预期导出。
请注意,当我使用时source
,环境变量不会被导出,但当我使用时.
它会被导出。
答案1
它们完全相同,如下所述man bash
:
. filename [arguments]
source filename [arguments]
Read and execute commands from filename in the current shell
environment and return the exit status of the last command executed
from filename. If filename does not contain a slash, file names in
PATH are used to find the directory containing filename. The file
searched for in PATH need not be executable. When bash is not in
posix mode, the current directory is searched if no file is found in
PATH. If the sourcepath option to the shopt builtin command is turned
off, the PATH is not searched. If any argu‐ ments are supplied, they
become the positional parameters when filename is executed. Otherwise
the positional parameters are unchanged. The return status is the
status of the last command exited within the script (0 if no commands
are executed), and false if filename is not found or cannot be read.
这里的问题source
是狂欢事情,标准实际上是.
。您的.profile
仅由登录 shell 和一些(并非所有)登录管理器读取。但是,登录管理器(例如 lightdm)将尝试使用系统的默认 shell 读取(源)文件,通常是/bin/sh
。在 Debian 派生的系统上,/bin/sh
是符号链接,/bin/dash
并且dash
是一个非常简单的、符合 POSIX 标准的 shell,不是 bash
并且对关键字一无所知source
。
因此,该命令将被忽略,文件不会被引用,变量也不会被定义。举例来说:
$ cat foo
myvar='foo1'
$ source foo
$ echo $myvar
foo1
同样的事情在dash
:
$ echo $0
dash
$ source foo
dash: 11: source: not found
$ . ~/foo ## dash needs the full path
$ echo $myvar
foo1