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

标签 linux bash ubuntu command-line

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

相关文章:

wordpress - 从 WP-CLI 向 WordPress 帖子添加标签/类别

mysql - 如何隐藏用于访问 MySQL 的 bash 脚本中的密码/用户名?

linux - 在登录时执行 bash 并添加别名

ubuntu - tensorflow 1.13.1 没有检测到 gpu

ubuntu - LD_PRELOAD 没有可见效果

c++ - 如何解决 Linux 上的线程问题?

c - posix 互斥锁没有按预期工作

PHP正则表达式问题

linux - Debian 有单一来源来获取多个内核的 linux-headers 吗?

ubuntu - 如何与在另一个容器中运行的 docker 容器分离