gpt4 book ai didi

linux - 如何将 Linux 命令放在括号中?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:46:52 25 4
gpt4 key购买 nike

我对已阅读的答案有疑问 here .如果我想用

重定向 time 命令的输出
{ time somecommand ; } &> file.txt

为什么开头要有空格和分号?如果我只是使用有区别吗

(time somecommand) &> file.txt

最佳答案

And is there a difference if I just used (time somecommand) &> file.txt ?

不同之处在于 ( ... ) 中的命令在子 shell 中运行;这是一个不同的过程。然而,{...} 下的命令在相同 shell 中运行。来自 bash manual :

 (list) list is executed in a subshell environment (see COMMAND
EXECUTION ENVIRONMENT below). Variable assignments and
builtin commands that affect the shell's environment do not
remain in effect after the command completes. The return
status is the exit status of list.

对于复合命令中的空格,bash语法要求:

{ list; }
list is simply executed in the current shell environment.
list must be terminated with a newline or semicolon. This is
known as a group command. The return status is the exit
status of list. Note that unlike the metacharacters ( and ),
{ and } are reserved words and must occur where a reserved
word is permitted to be recognized. Since they do not cause a
word break, they must be separated from list by whitespace or
another shell metacharacter.

关于linux - 如何将 Linux 命令放在括号中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42885590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com