“源”有什么作用?

And*_*mbu 688 bash shell documentation

$ whatis source
source: nothing appropriate.
$ man source
No manual entry for source
$ source
bash: source: filename argument required
source: usage: source filename [arguments]
Run Code Online (Sandbox Code Playgroud)

它存在,并且可以运行。为什么 Ubuntu 中没有关于它的任何文档?它有什么作用?如何安装有关它的文档?

小智 543

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

句法

. filename [arguments]

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

  • @jlliagre 我个人的“解释为什么有来源”是,`source` 不仅更具描述性,而且看起来不像是错别字。当我在电子邮件中发送技术命令时,有人跳过了句点/点。 (28认同)
  • @nagul,`source` 没有出现在 Bourne shell 中,它是一个 GNU 扩展,后来才出现。原始且仍然可移植的语法 (POSIX) 是使用“点”命令,即`.`。我个人从不使用 `source`,因为它的输入时间更长并且没有附加价值。我想它的主要目的是使脚本对新手更具可读性。 (16认同)
  • `source` 是 bash 特定的命令还是其他 shell 也有它?(我要求在问题上正确标记......) (11认同)
  • 此命令的一个常见用途是将 shell 脚本用于“配置文件”中的 `source`,该文件主要包含变量赋值。变量赋值然后控制脚本的其余部分所做的事情。当然,一个好的脚本会在 `source` 之前将变量设置为合理的默认值,或者至少检查有效值。 (3认同)
  • Afaik,`source` 出现在 Bourne shell 中,因此可能出现在它的所有后代中。http://en.wikipedia.org/wiki/Bourne_shell。我知道并非所有 shell 都有 `source` 命令,不太确定哪些 shell 确实包含它。 (2认同)
  • 所以实际上,`.` 是原始命令,`source` 是它的同义词/别名。 (2认同)
  • @user855443 等价于 `source myscript` 是 `. myscript` 或 `. ./myscript` 如果脚本在您的当前目录中。 (2认同)

dam*_*hat 335

当心!./并且source不太一样

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

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

https://askubuntu.com/questions/182012/is-there-a-difference-between-and-source-in-bash-after-all?lq=1

  • 您正在混淆 ./command 和 . 脚本。source-command 与 .-command 相同。使用 ./meh 表示在当前目录中运行名为 meh 的脚本/二进制文件,与 source/ 无关。-命令。正如您的链接中的回答所解释的那样。 (30认同)
  • 接受的答案也指向这一点很重要,因为有一段时间我认为`./ == source == .` (11认同)
  • @JoakimElofsson 链接中提到了,但我会修改答案以避免误解。请更正。 (4认同)

小智 102

了解“type”命令很有用:

> type source
source is a shell builtin
Run Code Online (Sandbox Code Playgroud)

每当某些东西是内置的 shell 时,就该做man bash

  • 你也可以使用`help {builtin-name}`,即`help source`。 (24认同)
  • 放大:如果您正在使用 bash,并且如果您知道(可能通过“type”)它是一个内置命令,那么“help”将让您直接进入您想要的文档段落,而无需涉足 4,184 行 '男人 bash 的文字。 (4认同)
  • 阅读`man`时总是知道一些新的东西) (2认同)
  • `help` 并非在任何地方都有效(至少在 zsh 中)。`type` 确实如此。 (2认同)

Jaw*_*awa 43

. (a period) 是一个bash shell 内置命令,它在当前 shell 中从作为参数传递的文件中执行命令。“来源”是“.”的同义词。

从 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  exe?
       cuted from filename.  If filename does not contain a slash, file
       names in PATH are used to find the  directory  containing  file?
       name.   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 arguments are supplied, they become the posi?
       tional 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)


Joa*_*son 39

'source' 是 '.' 的长版本。命令。在 bash 提示符下,可以执行以下操作:

source ~/.bashrc
Run Code Online (Sandbox Code Playgroud)

为当前正在运行的 bash 重新加载您的(已更改?)bash 设置。

简短版本将是:

. ~/.bashrc
Run Code Online (Sandbox Code Playgroud)

手册页:

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


小智 32

sourcecommand在当前shell 环境中执行提供的脚本(可执行权限不是强制性的,而在新的shell 中执行提供的可执行脚本。./

source命令确实有一个同义词. filename

为了更清楚,请查看以下设置别名的脚本。

别名

#! /bin/bash

alias myproject='cd ~/Documents/Projects/2015/NewProject'
Run Code Online (Sandbox Code Playgroud)

现在我们有两个选择来执行这个脚本。但是只有一个选项,可以在这两个选项中创建当前 shell 所需的别名。

选项1: ./make_alias

首先使脚本可执行。

chmod +x make_alias
Run Code Online (Sandbox Code Playgroud)

执行

./make_alias
Run Code Online (Sandbox Code Playgroud)

核实

alias
Run Code Online (Sandbox Code Playgroud)

输出

**nothing**
Run Code Online (Sandbox Code Playgroud)

哎呀!别名随新外壳一起消失了。

让我们选择第二个选项。

选项 2: source make_alias

执行

source make_alias
Run Code Online (Sandbox Code Playgroud)

或者

. make_alias
Run Code Online (Sandbox Code Playgroud)

核实

alias
Run Code Online (Sandbox Code Playgroud)

输出

alias myproject='cd ~/Documents/Projects/2015/NewProject'
Run Code Online (Sandbox Code Playgroud)

是的,别名已设置。


小智 10

如有疑问,最好的办法是使用以下info命令:

[root@abc ~]# info source

BASH BUILTIN COMMANDS
       Unless otherwise noted, each builtin command documented in this section
       as accepting options preceded by - accepts -- to signify the end of the
       options.   The  :, true, false, and test builtins do not accept options
       and do not treat -- specially.  The exit, logout, break, continue, let,
       and  shift builtins accept and process arguments beginning with - with-
       out requiring --.  Other builtins that accept  arguments  but  are  not
       specified  as accepting options interpret arguments beginning with - as
       invalid options and require -- to prevent this interpretation.
       : [arguments]
              No effect; the command does nothing beyond  expanding  arguments
              and  performing any specified redirections.  A zero exit code is
              returned.

        .  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 exe-
              cuted from filename.  If filename does not contain a slash, file
              names  in  PATH  are used to find the directory containing file-
              name.  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 arguments are supplied, they become the  posi-
              tional  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)


Ale*_*ira 7

来自 Linux 文档项目,高级 Bash 脚本指南,
第 15 章 -内部命令和内置

来源(点命令):
当从命令行调用此命令时,将执行脚本。在脚本中,源文件名加载文件文件名。获取文件(点命令)将代码导入脚本,附加到脚本(与 C 程序中的 #include 指令效果相同)。最终结果与“来源”代码行实际存在于脚本主体中一样。这在多个脚本使用公共数据文件或函数库的情况下很有用。
如果源文件本身是一个可执行脚本,那么它将运行,然后将控制权返回给调用它的脚本。为此,源可执行脚本可能会使用返回值。

因此,对于那些熟悉 C 编程语言的人来说,获取文件的效果类似于#include指令。

另请注意,您可以将位置参数传递给正在获取的文件,例如:

$ source $filename $arg1 arg2
Run Code Online (Sandbox Code Playgroud)

  • 我添加了另一个信息来源和之前未提及的其他信息。 (3认同)
  • 我不知道 `source` 可以带参数或使用 `return`。 (2认同)

小智 6

在 shell 中键入命令“help source”。

你会得到这样的输出:

source: source filename [arguments]

Execute commands from a file in the current shell.

Read and execute commands from FILENAME in the current shell.  The
entries in $PATH are used to find the directory containing FILENAME.
If any ARGUMENTS are supplied, they become the positional parameters
when FILENAME is executed.

Exit Status:
Returns the status of the last command executed in FILENAME; fails if
FILENAME cannot be read.
Run Code Online (Sandbox Code Playgroud)


小智 5

应该指出的是,尽管这是一个很棒的命令,但source它及其简写都.不会来源 多个文件,这意味着

source *.sh
Run Code Online (Sandbox Code Playgroud)

或者

. script1.sh script2.sh
Run Code Online (Sandbox Code Playgroud)

不管

我们可以使用循环回退for,但它会多次发出可执行文件,创建多个命令或发出多个命令。

结论:source不接受多个文件作为输入。参数必须是一个。

恕我直言,这很糟糕。