Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
722 views
in Technique[技术] by (71.8m points)

bash - Redirect outputs of multiple commands to a file

I'm running multiple commands in my Linux shell at the same time, e.g.

echo "Line of text 1" && echo "Line of text 2" && complexthing | xargs printf "complexspecifier"

I want to redirect all output to file1. I know I can add >file1 after each individual command but this seems bulky. How can I do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
exec >file1   # redirect all output to file1
echo "Line of text1"
echo "Line of text2"
exec > /dev/tty  # direct output back to the terminal 

Or, if you are on a machine that doesn't have /dev/tty, you can do:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...