我有一个问题我已经阅读了here。如果我想用
重定向time
命令的输出
{ time somecommand ; } &> file.txt
为什么开头的空格和分号是必需的?如果我刚用
,会有区别吗?(time somecommand) &> file.txt
答案 0 :(得分:2)
如果我刚刚使用(时间某些命令)&>那会有区别吗? 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.