替换 sh 中的源
我需要设置环境变量,通常我们通过以下方式执行此操作
source script.sh
,但现在,我在启动过程中将其自动化,看起来默认情况下 root 使用 sh
shell 启动。如何在 sh
中获取此脚本?
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
点命令“
.
”相当于 C Shell(和 Bash)source
命令。它由 POSIX 指定(请参阅dot
),并由 Bourne 和 Korn shell(我相信还有zsh
)支持。请注意,shell 使用
$PATH
查找文件,但该文件只需可读,而不可执行。正如下面的注释中所述,您当然可以指定文件的相对或绝对路径名 - 任何包含斜杠的名称都不会使用
$PATH
进行搜索。因此:如果
somefile
存在于三个不同的指定位置,则都可以用于查找该文件(如果您可以将.
替换为ls -l
并且请参阅列出的文件)。全世界学究联合起来!是的,如果当前目录是根目录,那么
/some/where/somefile
和./some/where/somefile
将引用同一个文件 - 具有相同的内容真实路径 - 即使没有链接,符号链接或硬链接,也会发挥作用(../../some/where/somefile
)。The dot command '
.
' is the equivalent of the C Shell (and Bash)source
command. It is specified by POSIX (seedot
), and supported by the Bourne and Korn shells (andzsh
, I believe).Note that the shell looks for the file using
$PATH
, but the file only has to be readable, not executable.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:could all be used to find
somefile
if it existed in the three different specified locations (if you could replace.
withls -l
and see a file listed).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
).tl;dr 对于
sh
(与bash
相对),参数必须包含斜杠:source ./script.sh,而不仅仅是
source script.sh
。除非可以在PATH
中找到script.sh
。点 (
.
) 命令存在于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/Bourne-Shell-Builtins.html#index-_002e
来自 POSIX:
tl;dr With
sh
(as opposed tobash
) the argument must contain a slash:source ./script.sh
, not justsource script.sh
. Unlessscript.sh
can be found inPATH
.Dot (
.
) command exists in bothbash
andsh
. Additionally,bash
aliases it assource
. Frombash
manual:https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html#index-source
https://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html#index-_002e
From POSIX: