我对已阅读的答案有疑问 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/