将多个命令的输出重定向到文件

时间:2017-05-26 01:50:42

标签: bash shell command-line command output

我同时在我的Linux shell中运行多个命令,例如

Bootstrap 3.0.0

我想将所有输出重定向到echo "Line of text 1" && echo "Line of text 2" && complexthing | xargs printf "complexspecifier" 。我知道我可以在每个单独命令之后添加file1但这看起来很笨重。我怎么能这样做?

3 个答案:

答案 0 :(得分:6)

exec >file1   # redirect all output to file1
echo "Line of text1"
echo "Line of text2"
exec > /dev/tty  # direct output back to the terminal 

或者,如果您使用的是没有/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

答案 1 :(得分:6)

如果您不需要在子shell中运行命令,可以使用{ ... } > file

{ echo "Line of text 1" && echo "Line of text 2" && complexthing | xargs printf "complexspecifier"; } > file1

请注意,{之后需要一个空格,}之前需要一个分号,除非在最后一个命令之后有&或换行符。

答案 2 :(得分:1)

想出来。您可以在命令周围使用括号,然后附加>file1

(echo "Line of text 1" && echo "Line of text 2" && complexthing | xargs printf "complexspecifier") >file1
相关问题