Senger CodeLab ๐Ÿš€

How to pipe stdout while keeping it on screen and not to a output file

September 29, 2026

๐Ÿ“‚ Categories: Bash
How to pipe stdout while keeping it on screen  and not to a output file

Redirecting command output is a fundamental aspect of shell scripting and command-line mastery. Often, you’ll want to pipe output to another command for further processing. But what if you need to see the output on your screen and pipe it to another process simultaneously? This seemingly simple task can be surprisingly tricky for newcomers. This guide will delve into various techniques to achieve this, explaining the underlying mechanics and providing practical examples. We’ll explore solutions using tee, process substitution, and named pipes, empowering you to manage your command-line workflow efficiently.

The Power of tee

tee is the most common solution for this problem. It acts like a T-junction in plumbing, reading from standard input and writing to both standard output and one or more files. This allows you to see the output on your terminal while simultaneously saving it to a file. For our purpose, instead of a file, we can redirect tee’s output to another command.

For example, let’s say you want to run a long-running command, see its output in real-time, and also save it to a log file. You would use:

command | tee logfile.txt

To pipe to another command while displaying output, use process substitution:

command | tee >(another_command)

Process Substitution: A Deeper Dive

Process substitution is a shell feature (Bash, Zsh) that treats the output of a command as a temporary file. This temporary file can then be passed as input to another command. It’s an elegant way to achieve our goal without creating actual intermediate files. The syntax is >(command) for output substitution and <(command) for input substitution.

Consider this example: ls -l | tee >(wc -l). This command lists all files and directories, pipes the output to tee, which then displays it on the terminal and simultaneously sends it to wc -l to count the number of lines.

This is particularly useful for debugging complex pipelines, allowing you to inspect intermediate stages of data processing without interrupting the flow.

Named Pipes: Advanced Control

Named pipes (FIFOs) offer more granular control, particularly in scenarios involving asynchronous processes. They act as virtual files that processes can read from and write to. Creating a named pipe with mkfifo mypipe allows you to write to it from one process and read from it in another, independent of each other.

For instance:

 mkfifo mypipe command > mypipe & cat mypipe | another_command 

This example runs command in the background, writing its output to mypipe. Simultaneously, cat mypipe reads from the pipe and sends the output to another_command. This allows for asynchronous processing and provides flexibility in managing complex data flows. Choosing the Right Approach

Selecting the appropriate method depends on the specific use case. tee is straightforward for simple scenarios where you need to see the output and save it to a file or pipe it to a single command. Process substitution is ideal for debugging and chaining commands within a single pipeline. Named pipes excel in situations requiring asynchronous processing and complex data flow management. Understanding the nuances of each technique empowers you to choose the most effective solution.

  • Simplicity: tee offers the easiest implementation.
  • Flexibility: Named pipes provide the most control.
  1. Identify your core need (displaying output, saving to file, piping to another command).
  2. Choose the appropriate technique based on complexity and requirements.
  3. Implement the chosen solution, testing its functionality thoroughly.

As an expert in systems administration, I often use these techniques to monitor server logs in real time while simultaneously archiving them for later analysis. This provides immediate feedback on system performance and retains historical data for troubleshooting. - John Doe, Senior Systems Administrator

Learn more about shell scripting.Featured Snippet: To display stdout and pipe it simultaneously, tee is the simplest solution. For more complex scenarios involving asynchronous operations, named pipes offer greater flexibility.

GNU Coreutils tee manual

Advanced Bash-Scripting Guide: Process Substitution

Linux man page for pipe(7)

Infographic Placeholder

[Infographic depicting the different methods visually]

FAQ

Q: Can I use tee with multiple pipes?

A: Yes, you can use tee with multiple pipes by using process substitution multiple times. For example: command | tee >(command1) >(command2)

Mastering these techniques will significantly enhance your command-line proficiency. By understanding the strengths and weaknesses of tee, process substitution, and named pipes, you can optimize your workflows for efficiency and clarity. Experiment with the examples provided, and explore further resources to deepen your understanding of shell scripting and command-line tools. These fundamental concepts will undoubtedly serve you well in various scripting and automation tasks. Consider incorporating these methods into your daily workflow to streamline your processes and gain valuable insights from your data. Explore more advanced shell scripting techniques to further enhance your command-line prowess and automate complex tasks efficiently. This opens a gateway to powerful automation and efficient data manipulation within your terminal environment.

Question & Answer :
I would like to pipe standard output of a program while keeping it on screen.

With a simple example (echo use here is just for illustration purpose) :

$ echo 'ee' | foo
ee <- the output I would like to see

I know tee could copy stdout to file but that’s not what I want.
$ echo 'ee' | tee output.txt | foo

I tried
$ echo 'ee' | tee /dev/stdout | foo but it does not work since tee output to /dev/stdout is piped to foo

Here is a solution that works at on any Unix / Linux implementation, assuming it cares to follow the POSIX standard. It works on some non Unix environments like cygwin too.

echo 'ee' | tee /dev/tty | foo 

Reference: The Open Group Base Specifications Issue 7 IEEE Std 1003.1, 2013 Edition, ยง10.1:

/dev/tty

Associated with the process group of that process, if any. It is useful for programs or shell procedures that wish to be sure of writing messages to or reading data from the terminal no matter how output has been redirected. It can also be used for applications that demand the name of a file for output, when typed output is desired and it is tiresome to find out what terminal is currently in use. In each process, a synonym for the controlling terminal

Some environments like Google Colab have been reported not to implement /dev/tty while still having their tty command returning a usable device. Here is a workaround:

tty=$(tty) echo 'ee' | tee $tty | foo 

or with an ancient Bourne shell:

tty=`tty` echo 'ee' | tee $tty | foo