“源”命令有什么作用?

2 unix bash shell

我想知道该命令的source作用是什么。我努力了:

  • 什么是
$ whatis source
source: nothing appropriate.
Run Code Online (Sandbox Code Playgroud)
  • 男人
$ man source
No manual entry for source
Run Code Online (Sandbox Code Playgroud)
  • 来源(-h、--help 等...)
$ source
source: not enough arguments
Run Code Online (Sandbox Code Playgroud)

但似乎没有关于它的文档。

我通常用它来保存点文件上的任何更改,但它到底有什么作用呢?为什么没有关于它的文档?

Abr*_*dez 6

source是一个 bash shell 内置命令,它在当前 shell 中执行作为参数传递的文件的内容。它有一个同义词.(句号)。

句法

. filename [arguments]

source filename [arguments]
Run Code Online (Sandbox Code Playgroud)

来自源码手册

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 short
    builtin command is turned off, the PATH is not searched. If any arguments
    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不太一样

  • ./script将脚本作为可执行文件运行,启动一个新的 shell来运行它
  • source script在当前 shell环境中从 filename 读取并执行命令

注意:./script不是. script,而是. script==source script

bash 中的源代码到底有什么区别吗?