bash 替换 sh 中的源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4732200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replacement for source in sh
提问by vkris
I need to set the environment variables, usually we do this by
我需要设置环境变量,通常我们这样做
source script.sh
But now, I am automating it during the boot process and it looks like the root boots by default with sh
shell. How do I source this script in sh
?
但是现在,我在引导过程中将其自动化,默认情况下它看起来像 root 引导时使用sh
shell。我如何在sh
.
回答by Jonathan Leffler
The dot command '.
' is the equivalent of the C Shell (and Bash) source
command. It is specified by POSIX (see dot
), and supported by the Bourne and Korn shells (and zsh
, I believe).
点命令“ .
”等效于 C Shell(和 Bash)source
命令。它由 POSIX(请参阅 参考资料dot
)指定,并由 Bourne 和 Korn shell 支持(zsh
我相信还有 )。
. somefile
Note that the shell looks for the file using $PATH
, but the file only has to be readable, not executable.
请注意,shell 使用 查找文件$PATH
,但该文件只需可读,而不是可执行文件。
As noted in the comments below, you can of course specify a relative or absolute pathname for the file — any name containing a slash will not be searched for using $PATH
. So:
正如下面的评论中所指出的,您当然可以为文件指定一个相对或绝对路径名——任何包含斜杠的名称都不会使用$PATH
. 所以:
. /some/where/somefile
. some/where/somefile
. ./somefile
could all be used to find somefile
if it existed in the three different specified locations (if you could replace .
with ls -l
and see a file listed).
都可以用来发现somefile
,如果它在三个不同的指定位置存在(如果你能代替.
使用ls -l
,并看到列出的文件)。
Pedants of the world unite! Yes, if the current directory is the root directory, then /some/where/somefile
and ./some/where/somefile
would refer to the same file — with the same real path — even without links, symbolic or hard, playing a role (and so would ../../some/where/somefile
).
全世界学士联合起来!是的,如果当前目录是根目录,那么/some/where/somefile
and./some/where/somefile
将引用同一个文件——具有相同的真实路径——即使没有链接,符号或硬链接,也有作用(也是如此../../some/where/somefile
)。
回答by x-yuri
tl;drWith sh
(as opposed to bash
) the argument must contain a slash: source ./script.sh
, not just source script.sh
. Unless script.sh
can be found in PATH
.
tl;dr与sh
(而不是bash
)参数必须包含斜杠:source ./script.sh
,而不仅仅是source script.sh
. 除非script.sh
可以在PATH
.
Dot (.
) command exists in both bash
and sh
. Additionally, bash
aliases it as source
. From bash
manual:
点 ( .
) 命令存在于bash
和 中sh
。此外,将其bash
别名为source
. 从bash
手册:
https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html#index-source
https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html#index-source
source
source filename
A synonym for
.
(see Bourne Shell Builtins).
来源
source filename
的同义词
.
(参见 Bourne Shell Builtins)。
https://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html#index-_002e
https://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html#index-_002e
. (a period)
. filename [arguments]
Read and execute commands from the
filename
argument in the current shell context. Iffilename
does not contain a slash, thePATH
variable is used to find filename. When Bash is not in POSIX mode, the current directory is searched iffilename
is not found in$PATH
.
. (一段时间)
. filename [arguments]
从
filename
当前 shell 上下文中的参数读取和执行命令。如果filename
不包含斜杠,则该PATH
变量用于查找文件名。当 Bash 不处于 POSIX 模式时,如果filename
在$PATH
.
From POSIX:
从POSIX:
If
file
does not contain a/
, the shell shall use the search path specified byPATH
to find the directory containing file. Unlike normal command search, however, the file searched for by the dot utility need not be executable.
如果
file
不包含/
,shell 将使用 指定的搜索路径PATH
来查找包含文件的目录。然而,与普通命令搜索不同,dot 实用程序搜索的文件不需要是可执行的。