Pipe Data Between Programs in Linux
Learn how Linux pipes connect command output to another command's input, with practical ps, sort, and tail pipeline examples.
A Linux pipeline connects two or more commands so that data flows from one command to the next. The shell's pipe operator is the vertical bar character, |.
For example:
command1 | command2
The command on the left runs first as the data source. Its output is passed directly to the command on the right, which processes that data as input. This lets you compose small programs, with each program performing one focused stage of a larger task.
Standard Input and Standard Output
Most command-line programs use streams for communication. Standard output, often called stdout, is the default stream where a command writes its results. Standard input, or stdin, is the default stream from which a program reads incoming data.
A pipe connects the left command's stdout to the right command's stdin:
left command stdout | right command stdin
This is different from saving output in a file. A pipe transfers data between commands while they run. Output redirection writes data to a file, and input redirection reads data from a file. A pipe by itself does not create a saved copy of the results.
Basic Pipe Syntax
Place the pipe operator between complete commands, with a command on both sides:
command1 | command2
Data flows from left to right. The second command receives and processes the results produced by the first command. Spaces around | make pipelines easier to read, although the shell does not require spaces in every context.
List Processes and Sort the Result
The ps command displays process information. The -A option asks for a process listing that includes processes across the system:
ps -A
To sort the process records, pipe the output to sort:
ps -A | sort -k 4
ps -Agenerates the process records.|transfers those records to the next command's standard input.sort -k 4orders the lines using the fourth whitespace-separated field as its sort key.
A sort key is the field, or portion of each line, that determines ordering. In the commonly illustrated ps -A format, the columns are similar to PID TTY TIME CMD, making the fourth field the executable or command-name field. Column layouts can vary, so inspect your actual output before relying on a particular field number.
Build a Multi-Command Pipeline
Several commands can be connected with multiple pipe operators:
ps -A | sort -k 4 | tail
Here, tail receives the sorted lines and displays the ending lines of its input. By default, tail displays the final ten lines.
ps -A -- process listing
| -- pass the listing onward
sort -k 4 -- sort by field four
| -- pass the sorted listing onward
tail -- select and display the final ten lines
Terminal-Style Data Flow
$ ps -A | sort -k 4 | tail
|
v
+----------------+
| ps -A |
| creates lines |
+----------------+
| process records
v
+----------------+
| sort -k 4 |
| orders by |
| field four |
+----------------+
| sorted lines
v
+----------------+
| tail |
| selects final |
| 10 lines |
+----------------+
|
v
CMD
cron
sshd
systemd
terminal
The sample names represent the kind of command-name field that may appear in process output. The important sequence is that sorting happens before tail selects the final lines. The final displayed result is therefore determined by tail, but the contents of those final lines depend on the ordering produced by sort.
Pipeline Stages and Data Flow
| Command stage | Purpose | Input received | Output passed onward |
|---|---|---|---|
ps -A | List processes across the system | Normally no piped input is needed | Process records written to standard output |
sort -k 4 | Order records by the fourth field | Process records from ps -A | Sorted process records |
tail | Show the ending lines | Sorted records from sort | The final ten lines by default |
Commands and Relevant Options
| Command or option | Function in the example |
|---|---|
ps -A | Produces the source process listing. |
sort | Orders lines of text. |
-k 4 | Selects the fourth field as the sort key. |
tail | Outputs the ending lines of its input, ten by default. |
| | Connects the previous command's standard output to the next command's standard input. |
Read and Verify Pipeline Output
When reading a pipeline, identify each stage from left to right:
- Find the command that creates the original data.
- Determine how each middle command transforms or filters that data.
- Look at the final command to understand what is displayed.
For ps -A | sort -k 4 | tail, ps -A creates the list, sort -k 4 changes its order, and tail displays only the last ten lines of that reordered list.
When a result is unexpected, inspect each stage separately:
ps -A
ps -A | sort -k 4
ps -A | sort -k 4 | tail
This makes it easier to determine whether the process listing, the selected sort field, or the final-line selection caused the result.
Troubleshooting Common Problems
The pipe is missing or misplaced
Put | between complete commands. A pipeline needs a valid command on both its left and right sides:
ps -A | sort -k 4
The output is not sorted as expected
Run ps -A by itself and inspect its whitespace-separated fields. Confirm that the intended value is in field four before using sort -k 4. A different output format may require a different key.
tail hides most of the data
That is its purpose: tail shows only the ending ten lines by default. Remove it temporarily to inspect the complete sorted result:
ps -A | sort -k 4
The results were expected to be saved
A pipe transfers data between running commands; it does not save the result to a file. Saving output requires a separate output-redirection operation. For related shell concepts, see Bourne Again Shell Bash.
Key Points
- A pipe is a shell mechanism for sending one command's stdout to another command's stdin.
- The pipe operator is
|, and data flows from left to right. ps -A | sort -k 4lists processes and sorts the records by field four.ps -A | sort -k 4 | tailsorts the complete listing, then displays its final ten lines.- Run individual stages when you need to inspect intermediate output.