Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

bash - Pipe multiple commands into a single command

How can I pipe the stdout of multiple commands to a single command?

Example 1: combine and sort the output of all three echo commands:

echo zzz; echo aaa; echo kkk

desired output:

aaa
kkk
zzz

Example 2: rewrite the following so that all the commands are in a single command-line using pipes, without redirects to a temp file:

setopt > /tmp/foo; unsetopt >> /tmp/foo; set >> /tmp/foo; sort /tmp/foo
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Use parentheses ()'s to combine the commands into a single process, which will concatenate the stdout of each of them.

Example 1 (note that $ is the shell prompt):

$ (echo zzz; echo aaa; echo kkk) | sort
aaa
kkk
zzz


Example 2:

(setopt; unsetopt; set) | sort

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...