简体   繁体   English

在bash中,dot命令&符号是做什么的?

[英]In bash, what does dot command ampersand do?

I'm trying to understand a bash script I'm supposed to be maintaining and got stuck. 我试图理解一个bash脚本,我应该维护并卡住。 The command is of this form: 命令是这种形式:

. $APP_LOCATION/somescript.sh param1 param2 &

The line is not being called in a loop, not is any return code bening sent back to the calling script from somescript.sh 该行不是在循环中调用的,也不是从somescript.sh发送回调用脚本的任何返回代码

I know that the "." 我知道“。” will make the process run in the same shell. 将使进程在同一个shell中运行。 But "&" will spawn off a different process. 但是“&”会产生不同的过程。

That sounds contradictory. 这听起来很矛盾。 What's is really happening here? 这里到底发生了什么? Any ideas? 有任何想法吗?

The script is running in a background process, but it is a subshell, not a separately-invoked interpreter as it would be without the dot. 该脚本在后台进程中运行,但它是一个子shell,而不是一个单独调用的解释器,因为它没有点。

That is to say -- the current interpreter forks and then begins running the command (sourcing the script). 也就是说 - 当前的解释器分叉然后开始运行命令(获取脚本)。 As such, it inherits shell variables, not just environment variables. 因此,它继承了shell变量,而不仅仅是环境变量。

Otherwise the new script's interpreter would be invoked via an execv() call, which would replace the current interpreter with a new one. 否则,将通过execv()调用调用新脚本的解释器,该调用将用新的解释器替换当前解释器。 That's usually the right thing, because it provides more flexibility -- you can't run anything but a script written for the same shell with . 这通常是正确的,因为它提供了更多的灵活性 - 除了为同一个shell编写的脚本之外,你不能运行任何东西. or source , after all, whereas starting a new interpreter means that your other script could be rewritten in Python, Perl, a compiled binary, etc without its callers needing to change. 或者source ,毕竟,启动一个新的解释器意味着你的另一个脚本可以用Python,Perl,编译的二进制文件等重写,而不需要调用者进行更改。

(This is part of why scripts intended to be exec'd, as opposed to than libraries meant to be sourced, should not have filename extensions -- and part of why bash libraries should be .bash , not .sh , such that inaccurate information isn't provided about what kind of interpreter they can be sourced into). (这是为什么要执行脚本的原因的一部分,而不是意图来源的库,不应该有文件扩展名 - 为什么bash库应该是.bash ,而不是.sh ,这样不准确的信息没有提供他们可以获得什么样的口译员)。

TL;DR TL; DR

. $APP_LOCATION/somescript.sh param1 param2 &

This sources a script as a background job in the current shell. 这会将脚本作为当前shell中的后台作业来源。

Sourcing a Script 采购脚本

In Bash, using . 在Bash中,使用. is equivalent to the [source builtin]. 相当于[source builtin]。 The help for the source builtin says (in part): 源内置的帮助说(部分):

$ help source
source: source filename [arguments]
    Execute commands from a file in the current shell.

In other words, it reads in your Bash script and evaluates it in the current shell rather than in a sub-shell. 换句话说,它读入您的Bash脚本并在当前shell中而不是在子shell中对其进行评估。 This is often important to give a script access to unexported variables. 这对于让脚本访问未导出的变量通常很重要。

Background Jobs 后台工作

The ampersand executes the script in the background using job control . &符号使用作业控制在后台执行脚本。 In this case, while the sourced script is evaluated in the context of the current shell, it is executed in a separate process that can be managed using job control builtins . 在这种情况下,虽然在当前shell的上下文中评估源脚本,但它是在可以使用作业控件内置管理的单独进程中执行的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM