Skip to main content

Redirecting Standard Input (stdin) in Linux

In the realm of Unix-like operating systems such as Linux, input/output redirection is a powerful feature that enables the manipulation of data streams coming from or going to a program. One of the critical components of this system is Standard Input (stdin), a data stream where a program reads its input. While redirecting Standard Output (stdout) and Standard Error (stderr) is more commonly demonstrated, stdin also has its set of redirection capabilities that are often underestimated. This article will shed light on redirecting stdin in Linux, with a particular focus on using the cat command.

What is stdin?

Standard Input is one of the three primary data streams used in Linux and other Unix-like operating systems, the other two being Standard Output and Standard Error. Typically, stdin reads data from the terminal keyboard, but through the magic of redirection, it can read data from files or even other programs ( although we won't focus on the latter in this article).

The Basic Syntax for stdin Redirection

The basic operator used to redirect stdin is the < symbol. When used, it tells the shell to read input from a file instead of the keyboard. The general syntax is as follows:

command < file

Redirecting stdin with the cat Command

The cat command is often used to concatenate and display the content of files. By default, it reads from stdin. This makes cat an ideal candidate for demonstrating stdin redirection. Here are some examples:

Basic Redirection of stdin

Instead of running cat and then manually typing input, you can redirect the input to come from a file:

cat < input.txt

This will display the content of input.txt on the terminal, just as if you'd run cat input.txt. However, this example demonstrates stdin redirection, not simply reading a file argument.

Multiple Files into One

Suppose you have multiple text files that you want to merge into a single file. One way to accomplish this is to use cat with stdin redirection and stdout redirection in tandem:

cat < file1.txt > merged_file.txt
cat < file2.txt >> merged_file.txt

In this example, the first cat command reads file1.txt via stdin and writes it to merged_file.txt via stdout. The second cat command appends the content of file2.txt to merged_file.txt.

Toggle Display Settings

The cat command also comes with options like -n or -b to show line numbers or non-blank line numbers. You can combine these with stdin redirection:

cat -n < input.txt

This will display the content of input.txt on the terminal, but with line numbers added, enhancing readability.

Concatenating Files in a Specific Order

You can use multiple cat commands and redirect the final output to a file to concatenate them in a specific order:

(cat < file1.txt; cat < file2.txt; cat < file3.txt) > all_files.txt

Here, each cat command reads a different file via stdin, and then the collective output is redirected to all_files.txt.


Understanding how to redirect stdin in Linux is an important aspect of mastering the command line interface. The cat command serves as a versatile tool for various stdin redirection tasks, from reading and merging files to enhancing text visibility with line numbers. Once you grasp these fundamental techniques, you'll find that you have much more flexibility and power at your fingertips, opening the door to advanced script writing and data manipulation.

What Can You Do Next 🙏😊

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.

YouTube @cloudaffle