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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:05:34  来源:igfitidea点击:

Replacement for source in sh

bashshellsh

提问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 shshell. How do I source this script in sh?

但是现在,我在引导过程中将其自动化,默认情况下它看起来像 root 引导时使用shshell。我如何在sh.

回答by Jonathan Leffler

The dot command '.' is the equivalent of the C Shell (and Bash) sourcecommand. 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 somefileif it existed in the three different specified locations (if you could replace .with ls -land see a file listed).

都可以用来发现somefile,如果它在三个不同的指定位置存在(如果你能代替.使用ls -l,并看到列出的文件)。

Pedants of the world unite! Yes, if the current directory is the root directory, then /some/where/somefileand ./some/where/somefilewould 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/somefileand./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.shcan be found in PATH.

tl;drsh(而不是bash)参数必须包含斜杠:source ./script.sh,而不仅仅是source script.sh. 除非script.sh可以在PATH.

Dot (.) command exists in both bashand sh. Additionally, bashaliases it as source. From bashmanual:

点 ( .) 命令存在于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 filenameargument in the current shell context. If filenamedoes not contain a slash, the PATHvariable is used to find filename. When Bash is not in POSIX mode, the current directory is searched if filenameis not found in $PATH.

. (一段时间)

. filename [arguments]

filename当前 shell 上下文中的参数读取和执行命令。如果filename不包含斜杠,则该PATH变量用于查找文件名。当 Bash 不处于 POSIX 模式时,如果filename$PATH.

From POSIX:

POSIX

If filedoes not contain a /, the shell shall use the search path specified by PATHto 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 实用程序搜索的文件不需要是可执行的。