# Run the ( ... ) in the background concurrently with the foreground process
( sleep 1; echo background ) & echo foreground; wait
[1] 32301
foreground
background
[1]+ Done ( sleep 1; echo background )
# Run the ( ... ) and then if it's successful execute the next statement
( sleep 1; echo background ) && echo foreground; wait
background
foreground
让我们使用您的第一个命令,并在临时目录中运行它:
但是现在看一下目录:
这里发生的情况是该
tty
命令报告终端设备,例如/dev/pty0
当它在附加终端的情况下执行时,以及not a tty
其他情况下的消息。当它在后台 (&
) 作为子 shell (( … )
) 运行时,没有终端设备,因此输出被重定向到一个名为not a tty
.现在在第二种情况下,除了子shell没有在后台执行之外,一切都非常相似:
在这种情况下
tty
,在前台运行并且可以返回当前终端的设备名称,因此您可以在屏幕上看到输出。请注意,这
&&
是一个逻辑连接器,因此第二个命令只有在第一个命令返回“成功”(0 值)退出状态时才会运行。不要与 混淆&
,后者指示前面的命令在后台独立运行:根据您想要实现的目标,您可能需要考虑使用
/dev/tty
,甚至根本不重定向输出。