登录shell用什么方法读取/etc/profile?

Ms.*_*hin 2 shell bash shell-script

登录shell使用什么方法读取/etc/profile

ter*_*don 10

它是来源。执行和采购之间的区别在这篇文章中有解释。这里的重要区别在于,source 导致源文件中的命令在当前 shell 中运行。这意味着文件中定义的任何变量现在都可以在 shell 中使用。为了说明差异,请尝试以下操作:

$ cat foo        ## a simple file with a variable definition
var="hello"
$ chmod +x foo   ## make file executable
$ ./foo          ## execute
$ echo "$var"    ## var is not set in the parent shell

$ . foo          ## source
$ echo "$var"    ## var is now set in the parent shell
hello
Run Code Online (Sandbox Code Playgroud)

因此,由于/etc/profile需要能够影响从它读取的 shell,它是来源而不是执行的。

  • @Ruslan `source` 不可移植,它只是一些 shell 中 `.` 命令的别名。可移植的方式是`.`,所以我更喜欢在这里使用 shell-agnostic 命令。 (6认同)
  • 我认为在示例中最好使用 `source foo` 而不是 `。foo`,因为 1) 空间在 `.` 和 `foo` 之间可能不可见,2) `source` 比 `.` 更适合在手册页中搜索。在评论中,可以说`. foo` 是另一种说法。 (3认同)