MD *_* XF 1 bash shell command-line command output
我在Linux Shell中同时运行多个命令,例如
echo "Line of text 1" && echo "Line of text 2" && complexthing | xargs printf "complexspecifier"
Run Code Online (Sandbox Code Playgroud)
我想将所有输出重定向到file1。我知道我可以>file1在每个单独的命令之后添加,但这似乎很庞大。我怎样才能做到这一点?
exec >file1 # redirect all output to file1
echo "Line of text1"
echo "Line of text2"
exec > /dev/tty # direct output back to the terminal
Run Code Online (Sandbox Code Playgroud)
或者,如果您使用的机器没有/dev/tty,则可以执行以下操作:
exec 5>&1 > file1 # copy current output and redirect output to file1
echo foo
echo bar
exec 1>&5 5>&- # restore original output and close the copy
Run Code Online (Sandbox Code Playgroud)
如果您不需要在子Shell中运行命令,则可以使用{ ... } > file:
{ echo "Line of text 1" && echo "Line of text 2" && complexthing | xargs printf "complexspecifier"; } > file1
Run Code Online (Sandbox Code Playgroud)
请注意,除非在最后一个命令之后有或换行符,否则您需要在空格之后{和分号之前。}&