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中执行作为参数传递的文件的内容。它在.
(句号)中有一个同义词。
句法
Run Code Online (Sandbox Code Playgroud). filename [arguments] source filename [arguments]
dam*_*hat 335
当心!./
并且source
都不太一样。
./script
将脚本作为可执行文件运行,启动一个新的 shell来运行它source script
从当前 shell环境中的filename 读取并执行命令注意:./script
不是. script
,而是. script
==source script
小智 102
了解“type”命令很有用:
> type source
source is a shell builtin
Run Code Online (Sandbox Code Playgroud)
每当某些东西是内置的 shell 时,就该做man bash
。
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
source
command在当前shell 环境中执行提供的脚本(可执行权限不是强制性的),而在新的shell 中执行提供的可执行脚本。./
source
命令确实有一个同义词. filename
。
为了更清楚,请查看以下设置别名的脚本。
#! /bin/bash
alias myproject='cd ~/Documents/Projects/2015/NewProject'
Run Code Online (Sandbox Code Playgroud)
现在我们有两个选择来执行这个脚本。但是只有一个选项,可以在这两个选项中创建当前 shell 所需的别名。
./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)
哎呀!别名随新外壳一起消失了。
让我们选择第二个选项。
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)
来自 Linux 文档项目,高级 Bash 脚本指南,
第 15 章 -内部命令和内置:
来源,。(点命令):
当从命令行调用此命令时,将执行脚本。在脚本中,源文件名加载文件文件名。获取文件(点命令)将代码导入脚本,附加到脚本(与 C 程序中的 #include 指令效果相同)。最终结果与“来源”代码行实际存在于脚本主体中一样。这在多个脚本使用公共数据文件或函数库的情况下很有用。
如果源文件本身是一个可执行脚本,那么它将运行,然后将控制权返回给调用它的脚本。为此,源可执行脚本可能会使用返回值。
因此,对于那些熟悉 C 编程语言的人来说,获取文件的效果类似于#include
指令。
另请注意,您可以将位置参数传递给正在获取的文件,例如:
$ source $filename $arg1 arg2
Run Code Online (Sandbox Code Playgroud)
小智 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
不接受多个文件作为输入。参数必须是一个。
恕我直言,这很糟糕。
归档时间: |
|
查看次数: |
680603 次 |
最近记录: |