管道是否保证在任何 POSIX shell 中创建子 shell?
Is pipeline guaranteed to create a subshell in any POSIX shell?
此 shell 脚本的行为符合预期。
trap 'echo exit' EXIT
foo()
{
exit
}
echo begin
foo
echo end
这是输出。
$ sh foo.sh
begin
exit
这表明脚本在执行时退出foo
。
现在看下面的脚本。
trap 'echo exit' EXIT
foo()
{
exit
}
echo begin
foo | cat
echo end
这里唯一的区别是 foo
的输出被输送到 `cat.现在输出如下所示。
begin
end
exit
这表明脚本在执行foo
时没有退出,因为打印了end
。
我相信发生这种情况是因为在 bash 中管道导致子 shell 打开,所以 foo | cat
等同于 (foo) | cat
.
在任何 POSIX shell 中都能保证这种行为吗?我在 http://pubs.opengroup.org/onlinepubs/9699919799/ 的 POSIX 标准中找不到任何暗示管道必须通向子 shell 的内容。有人可以确认这种行为是否可靠吗?
在 2.12 Shell Execution Environment 中您会找到这句话:
A subshell environment shall be created as a duplicate of the shell environment, except that signal traps that are not being ignored shall be set to the default action. Changes made to the subshell environment shall not affect the shell environment. Command substitution, commands that are grouped with parentheses, and asynchronous lists shall be executed in a subshell environment. Additionally, each command of a multi-command pipeline is in a subshell environment; as an extension, however, any or all commands in a pipeline may be executed in the current environment. All other commands shall be executed in the current shell environment.
这道题的关键句在哪里
Additionally, each command of a multi-command pipeline is in a subshell environment; as an extension, however, any or all commands in a pipeline may be executed in the current environment
所以没有扩展名(bash
用于 lastpipe
之类的东西,我想,对于管道中的第一个元素也是如此,但显然不是或至少不总是)它看起来就像您可以假设管道的每个部分都有一个子外壳,但异常意味着您不能完全指望它。
此 shell 脚本的行为符合预期。
trap 'echo exit' EXIT
foo()
{
exit
}
echo begin
foo
echo end
这是输出。
$ sh foo.sh
begin
exit
这表明脚本在执行时退出foo
。
现在看下面的脚本。
trap 'echo exit' EXIT
foo()
{
exit
}
echo begin
foo | cat
echo end
这里唯一的区别是 foo
的输出被输送到 `cat.现在输出如下所示。
begin
end
exit
这表明脚本在执行foo
时没有退出,因为打印了end
。
我相信发生这种情况是因为在 bash 中管道导致子 shell 打开,所以 foo | cat
等同于 (foo) | cat
.
在任何 POSIX shell 中都能保证这种行为吗?我在 http://pubs.opengroup.org/onlinepubs/9699919799/ 的 POSIX 标准中找不到任何暗示管道必须通向子 shell 的内容。有人可以确认这种行为是否可靠吗?
在 2.12 Shell Execution Environment 中您会找到这句话:
A subshell environment shall be created as a duplicate of the shell environment, except that signal traps that are not being ignored shall be set to the default action. Changes made to the subshell environment shall not affect the shell environment. Command substitution, commands that are grouped with parentheses, and asynchronous lists shall be executed in a subshell environment. Additionally, each command of a multi-command pipeline is in a subshell environment; as an extension, however, any or all commands in a pipeline may be executed in the current environment. All other commands shall be executed in the current shell environment.
这道题的关键句在哪里
Additionally, each command of a multi-command pipeline is in a subshell environment; as an extension, however, any or all commands in a pipeline may be executed in the current environment
所以没有扩展名(bash
用于 lastpipe
之类的东西,我想,对于管道中的第一个元素也是如此,但显然不是或至少不总是)它看起来就像您可以假设管道的每个部分都有一个子外壳,但异常意味着您不能完全指望它。