2009-09-24 10:35:48 +0000 2009-09-24 10:35:48 +0000
604
604

源码是做什么的?

$ whatis source
source: nothing appropriate.
$ man source
No manual entry for source
$ source
bash: source: filename argument required
source: usage: source filename [arguments]

它是存在的,而且是可以运行的。为什么Ubuntu中没有任何关于它的文档?它是做什么的?我如何才能安装有关它的文档?

答案 (9)

289
289
289
2013-07-11 13:24:07 +0000

小心!./source不完全相同的

  • ./script以可执行文件的形式运行脚本,启动一个新的shell来运行
  • source script当前shell环境中的文件名中读取并执行命令

注意:./script不是. script,而是. script ==source script https://askubuntu.com/questions/182012/is-there-a-difference-between-and-source-in-bash-after-all?lq=1

92
92
92
2013-01-09 15:45:47 +0000

知道 “type "命令很有用:

> type source
source is a shell builtin

每当有东西是shell内建的时候,就该做man bash了。

40
40
40
2009-09-24 10:48:52 +0000

. (a period)是bash shell内置命令,在当前shell中执行从传递的文件作为参数的命令。'source'是’.‘的同义词。

来自 Bash man page:

. 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.
32
32
32
2009-09-24 10:46:01 +0000

‘source'是’.‘命令的长版本。在bash提示符上,可以做以下操作:

source ~/.bashrc

,重新加载当前运行的bash设置。

28
28
28
2015-03-27 13:57:39 +0000

source命令在当前的shell环境中执行所提供的脚本(可执行权限是非强制性的),而./新的shell中执行所提供的可执行的脚本。

source命令确实有一个同义词. filename。但只有一个选项,可以在这两个选项中创建当前shell所需的别名。 选项1:

选项1:./make_alias

执行脚本,先让脚本可执行。

10
10
10
2015-11-11 05:44:49 +0000

当有疑问时,最好使用 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.
5
5
5
2015-09-19 14:14:57 +0000

在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.
4
4
4
2017-06-12 00:13:34 +0000

摘自 Linux 文档项目,高级 Bash 脚本指南, 第 15 章 - [内部命令和内建]: (http://tldp.org/LDP/abs/html/internal.html):

source* , . (点命令): 这条命令在命令行中调用时,执行一个脚本。在一个脚本中,源文件名会加载文件的文件名。源文件(dot-command)将代码导入到脚本中,附加到脚本中(与C语言程序中的#include指令的效果相同)。这样做的结果就和 “源 "的代码行在脚本中的实际存在是一样的。这在多个脚本使用一个共同的数据文件或函数库的情况下非常有用。 如果源文件本身就是一个可执行的脚本,那么它将运行,然后将控制权返回给调用它的脚本。因此,对于熟悉C语言的人来说,源文件的作用类似于#include指令。

还需要注意的是,你可以向被源文件传递位置参数,比如:

$ source $filename $arg1 arg2
0
0
0
2018-11-28 08:58:53 +0000

有了源码,你可以将其他文件中的变量或函数传入到你的脚本中,而不需要再写一次就可以使用它们。