ThisCalculator

Everyday

Pipe Command Output to Other Commands? Explained

Updated · Standard formula, shown below

Average pace (min/km) 5.00 min/km
Average speed (km/h)
12.00 km/h
Result
Total time (min)
50 min
You entered
Distance (km)
10 km
You entered

Yes, you can pipe command output to other commands—this is a core feature of Unix-like shells (bash, zsh, fish) and Windows PowerShell. You connect commands with the pipe symbol (|), which sends the first command’s standard output (stdout) directly into the second command’s standard input (stdin), letting you chain small tools into powerful pipelines.

Formula

The pace formula is simply total time divided by distance. In symbols: pace = t / d, where t is the total time in minutes and d is the distance in kilometers. The result is minutes per kilometer. For speed, you invert that: speed = d / (t / 60), which converts minutes to hours, giving kilometers per hour.

Worked Example

Using the default values: t = 50 minutes, d = 10 km. First, pace = 50 / 10 = 5.0 min/km. For speed, t / 60 = 50 / 60 = 0.8333 hours, then 10 / 0.8333 = 12.0 km/h. So the average pace is 5 minutes per kilometer, and the average speed is 12 kilometers per hour.

Why Piping Matters

Piping is the glue of command-line productivity. Instead of writing one monolithic program, you combine small, focused tools—each does one thing well—and let the shell connect them. This philosophy, popularized by Unix, lets you filter logs, transform text, and automate repetitive tasks without opening a file or writing a script. For example, cat access.log | grep "404" | wc -l counts 404 errors in a log file. Each command is simple, but together they solve a real problem.

How Pipes Work Under the Hood

When you type cmd1 | cmd2, the shell creates a pipe—a unidirectional data channel with a read end and a write end. It forks two processes: cmd1’s stdout is redirected to the write end, and cmd2’s stdin is redirected from the read end. Data flows in chunks (typically 4KB or 64KB) through a kernel buffer. Both commands run concurrently, not sequentially, so cmd2 can start processing as soon as cmd1 produces output, which is why pipelines are fast and memory-efficient for large streams.

Choosing the Right Pipe Tool

Which command you pipe to depends on what you want to do with the output:

  • Filter lines: grep or rg (ripgrep) for pattern matching.
  • Transform text: sed for substitutions, awk for column extraction.
  • Sort and unique: sort, then uniq -c for counts.
  • Count or measure: wc -l for lines, wc -c for bytes.
  • Read paginated: less or more for long output.
  • Save to file: tee to see output and write it simultaneously.

For binary data or null-delimited records, use tr or xargs -0 to avoid word-splitting issues.

Common Pitfalls and How to Avoid Them

One frequent mistake is piping stderr—error messages go to stderr, not stdout, so they bypass the pipe. Redirect with 2>&1 to merge them. Another pitfall is forgetting that some commands buffer output when not connected to a terminal; use stdbuf -oL or grep --line-buffered to force line buffering. Also, beware of trailing spaces or carriage returns in Windows files—use dos2unix or tr -d '\r' to clean them. Finally, when piping to xargs, filenames with spaces break; use -0 with null separators.

Pipes vs. Redirection vs. Subshells

Pipes (|) connect two commands directly. Redirection (>, <) sends output to a file or reads input from one. Subshells ((...)) run commands in a separate environment, but you can combine all three: (cmd1; cmd2) | tee output.txt runs both commands in a subshell, pipes their combined output to tee, which displays it and writes to a file. Each technique has its place, but pipes are the most natural way to chain live data streams.

Performance and Safety Considerations

Pipelines are generally safe and efficient, but there are a few things to keep in mind. First, if any command in the pipeline fails, the exit status of the whole pipeline is the last command’s status by default; use set -o pipefail in bash to catch failures early. Second, be careful with infinite streams—if you pipe yes to head -n 5, the pipeline terminates when head exits, but the producer may get a SIGPIPE; that’s normal. Third, avoid piping sensitive data (like passwords) to commands that log arguments, as they may appear in process lists. Finally, for very large files, consider using awk or perl for in-place processing instead of multiple pipes, as each pipe adds a small overhead.

Frequently Asked Questions

How do I pipe both stdout and stderr?

Use 2>&1 before the pipe: cmd 2>&1 | less. This redirects stderr to stdout, so both streams go into the pipe. In PowerShell, use 2>&1 as well, or cmd 2>&1 | Out-String.

Why does my pipe not work with sudo?

Because the pipe is set up by the shell before sudo runs. For example, sudo cat /etc/shadow | grep root runs cat as root but grep as your user, which is fine. But sudo ls /root | less will show permission denied because ls runs as root, but the pipe is fine. The issue is usually that the command after the pipe doesn’t have root privileges; use sudo bash -c 'cmd1 | cmd2' if you need both to run as root.

Can I pipe to multiple commands at once?

Not directly with a single pipe. Use tee to split the stream: cmd | tee >(grep foo) >(grep bar) > /dev/null. This uses process substitution to send output to two greps simultaneously. Alternatively, use tee output.txt | cmd2 to save a copy while continuing the pipeline.

How do I pipe a file into a command?

Use input redirection: cmd < file.txt or cat file.txt | cmd. The first is more efficient because it avoids spawning cat. For multiple files, use cat file1 file2 | cmd.

What does ‘broken pipe’ mean?

It means the reading end of the pipe closed before the writing end finished. This happens when the downstream command exits early, like head after reading a few lines. The writer receives a SIGPIPE signal and terminates. It’s normal and usually harmless; you can ignore it or handle it in scripts with trap '' PIPE.

Related

Formula used
in t | Total time (min) | min | 50 | 0
in d | Distance (km) | km | 10 | 0
out pace | Average pace (min/km) | min/km | t / d | 2
out speed | Average speed (km/h) | km/h | d / (t / 60) | 2

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.