Connecting Two Programs by Using Output As Input

Problem

You want to take the output from one program and use it as the input of another program.

Solution

You could redirect the output from the first program into a temporary file, then use that file as input to the second program. For example:

$ cat one.file another.file > /tmp/cat.out
$ sort < /tmp/cat.out
...
$ rm /tmp/cat.out

Or you could do all of that in one step by sending the output directly to the next program by using the pipe symbol | to connect them. For example:

$ cat one.file another.file | sort

You can also link a sequence of several commands together by using multiple pipes:

$ cat my* | tr 'a-z' 'A-Z' | uniq | awk -f transform.awk | wc

Discussion

By using the pipe symbol we don’t have to invent a temporary filename, remember it, and remember to delete it.

Programs like sort can take input from standard in (redirected via the < symbol) but they can also take input as a filename—for example:

$ sort /tmp/cat.out

rather than redirecting the input into sort:

$ sort < /tmp/cat.out

That behavior (of using a filename if supplied, and if not, of using standard input) is a typical Unix/Linux characteristic, and a useful model to follow so that commands can be connected one to another via the pipe mechanism. If you write your programs and shell scripts that way, they will be more useful to you and to those with whom you share your work.

Feel free to be amazed at the powerful simplicity of the pipe mechanism. You can even think of the pipe as ...

Get bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.