首页 > 解决方案 > 命令'source'有什么作用?

问题描述

我想知道这个命令source是做什么的。我努力了:

$ whatis source
source: nothing appropriate.
$ man source
No manual entry for source
$ source
source: not enough arguments

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

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

标签: bashshellunix

解决方案


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

句法

. filename [arguments]

source 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 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. 

当心!./source不太一样

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

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

毕竟 bash 中的源代码有什么区别吗?


推荐阅读