源码是做什么的?
$ whatis source
source: nothing appropriate.
$ man source
No manual entry for source
$ source
bash: source: filename argument required
source: usage: source filename [arguments]
它是存在的,而且是可以运行的。为什么Ubuntu中没有任何关于它的文档?它是做什么的?我如何才能安装有关它的文档?
$ whatis source
source: nothing appropriate.
$ man source
No manual entry for source
$ source
bash: source: filename argument required
source: usage: source filename [arguments]
它是存在的,而且是可以运行的。为什么Ubuntu中没有任何关于它的文档?它是做什么的?我如何才能安装有关它的文档?
小心!./
和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
. (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.
‘source'是’.‘命令的长版本。在bash提示符上,可以做以下操作:
source ~/.bashrc
,重新加载当前运行的bash设置。
source
命令在当前的shell环境中执行所提供的脚本(可执行权限是非强制性的),而./
在新的shell中执行所提供的可执行的脚本。
source
命令确实有一个同义词. filename
。但只有一个选项,可以在这两个选项中创建当前shell所需的别名。 选项1:
./make_alias
当有疑问时,最好使用 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.
在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.
摘自 Linux 文档项目,高级 Bash 脚本指南, 第 15 章 - [内部命令和内建]: (http://tldp.org/LDP/abs/html/internal.html):
source* , . (点命令): 这条命令在命令行中调用时,执行一个脚本。在一个脚本中,源文件名会加载文件的文件名。源文件(dot-command)将代码导入到脚本中,附加到脚本中(与C语言程序中的#include指令的效果相同)。这样做的结果就和 “源 "的代码行在脚本中的实际存在是一样的。这在多个脚本使用一个共同的数据文件或函数库的情况下非常有用。 如果源文件本身就是一个可执行的脚本,那么它将运行,然后将控制权返回给调用它的脚本。因此,对于熟悉C语言的人来说,源文件的作用类似于
#include
指令。
还需要注意的是,你可以向被源文件传递位置参数,比如:
$ source $filename $arg1 arg2