Dan*_*iel 3 bash environment-variables
我的印象是它source是.bash的同义词。但是,似乎在.profile文件中,source不起作用。该 youtube 视频演示了当source用于~/.profile获取文件时foo,该文件中定义的变量不会导出到后续 shell。但是,如果文件是使用 获取的.,则变量将按预期导出。
请注意,当我使用时source,环境变量不会被导出,但是当我使用.它时。
它们完全相同,如中所述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.
Run Code Online (Sandbox Code Playgroud)
这里的问题是这source是一个bash 的东西,标准实际上是.. 您.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
Run Code Online (Sandbox Code Playgroud)
同样的事情dash:
$ echo $0
dash
$ source foo
dash: 11: source: not found
$ . ~/foo ## dash needs the full path
$ echo $myvar
foo1
Run Code Online (Sandbox Code Playgroud)