Kahibaro
Discord Login Register

2.3.4 Input/output redirection

Introduction

Input and output redirection lets you control where commands read data from and where they send their results. Instead of always using the keyboard for input and the terminal screen for output, you can use files, combine commands together, or discard unwanted output. This chapter introduces the core redirection operators you will use constantly on the Linux command line.

Standard input, output, and error

Every command you run has three standard data streams. Standard input is where the command reads data from, usually the keyboard. Standard output is where normal results are written, usually the terminal. Standard error is where error messages are written, also usually the terminal.

Internally these are numbered file descriptors: standard input is 0, standard output is 1, and standard error is 2. You rarely need the numbers for basic use, but some redirection syntax uses them.

Key standard streams
Standard input: file descriptor 0.
Standard output: file descriptor 1.
Standard error: file descriptor 2.

Redirecting standard output to a file

The simplest redirection operator is >. It takes the standard output of a command and writes it to a file instead of the terminal. If the file exists, it is overwritten.

For example, to save the current directory listing into a file named list.txt, run:

ls > list.txt

After this, nothing appears on the screen, but list.txt contains what ls would have printed. If list.txt already existed, its previous contents are lost.

You can also use an explicit file descriptor number, but for standard output it is usually optional. The following two commands are equivalent:

echo "Hello" > out.txt
echo "Hello" 1> out.txt

Appending output to a file

Sometimes you do not want to overwrite a file, but add to the end of it. For this you use >> instead of >.

To append to a log file named log.txt:

echo "Starting run" >> log.txt
date >> log.txt

Each command adds its output to the end of log.txt. The file is created if it does not already exist.

Again, you can write the file descriptor explicitly, so 1>> has the same effect as >>.

Redirecting standard input from a file

To make a command read its input from a file instead of the keyboard, use <. This connects the file to standard input.

For example, cat without arguments reads from standard input. You can feed it a file using <:

cat < myfile.txt

This prints the contents of myfile.txt to the terminal. For many programs, providing the file as an argument or using < has the same visible effect, but redirection can be important for commands that expect to read only from standard input.

You can use the numeric form 0< to be explicit:

sort 0< unsorted.txt

This sorts lines taken from unsorted.txt and prints the sorted result to standard output.

Combining input and output redirection

You can redirect both input and output in the same command. For example, to read from one file and write the processed result to another:

sort < unsorted.txt > sorted.txt

Here, standard input comes from unsorted.txt and standard output goes to sorted.txt. Nothing is printed to the terminal.

The order of the redirection operators on the command line does not matter to the shell, as long as they are all part of the same command.

Redirecting errors separately

By default, only standard output is affected by > and >>. Error messages still go to the terminal. This is often useful because you can see errors even when you capture normal output in a file.

If you want to redirect error messages, you must use 2> or 2>>. For example, to log only error messages from a command:

somecommand 2> errors.log

Here, standard output still goes to the terminal, but standard error goes to errors.log. To append errors instead of overwriting the file:

somecommand 2>> errors.log

You can redirect both standard output and standard error, each to its own place:

somecommand > out.log 2> err.log

This splits normal output and error messages into separate files.

Merging output and error

Sometimes you want both standard output and standard error to go to the same destination. There are two common patterns.

To send standard error to the same place as standard output, you use 2>&1. The order in which you write these redirections matters. First redirect standard output, then make standard error point to it:

somecommand > all.log 2>&1

This writes everything, both normal output and errors, into all.log.

You can also combine this with append:

somecommand >> all.log 2>&1

If you want to discard all error messages but keep normal output, send standard error to /dev/null, which is a special file that throws away anything written to it:

somecommand > result.txt 2> /dev/null

Discarding output

Sometimes you do not care about the output at all. You can send it to /dev/null to ignore it.

To run a command and see only its error messages, not its normal output:

somecommand > /dev/null

To ignore both normal output and errors:

somecommand > /dev/null 2>&1

Using /dev/null is common when you only care about whether a command succeeds, not what it prints.

Here documents

A here document lets you provide multi line input directly in the shell, without using a separate file. The operator is << followed by a word that marks the end of the input.

For example:

cat <<END
This is line one.
This is line two.
END

cat reads from standard input, but here standard input comes from the text you typed between <<END and the line containing only END. The word END is called the delimiter. You can choose any word, but it must match exactly at the end line.

This is useful to feed configuration snippets or small pieces of text into programs during scripting, without creating temporary files.

If you write the delimiter in quotes, such as <<'END', the shell does not expand variables or special characters in the here document text. Without quotes, variable names and commands may be expanded before the text is passed to the program.

Here strings

A here string is a shorter form that sends a single line of input to a command. It uses <<<.

For example:

tr 'a-z' 'A-Z' <<< "hello world"

In this command, the string "hello world" becomes the standard input of tr, which then prints HELLO WORLD. A here string is convenient when you want to test a command quickly with a small piece of input.

Redirection and command pipelines

Connecting commands with pipes is covered separately, but you will often mix pipes and redirection together. A pipe sends the standard output of one command directly into the standard input of another. You can still redirect final output or errors to files.

For example:

ls /nonexistent | sort > listing.txt 2> errors.txt

Here, ls sends its output into sort through a pipe. The sorted result goes to listing.txt, and any error messages from either command are written to errors.txt through the 2> redirection.

Pipes and redirection together let you build flexible chains of commands that read and write exactly where you want.

Summary

Input and output redirection gives you precise control over how commands interact with files and with each other. You can redirect standard output with > or >>, redirect standard input with <, capture or redirect errors with 2>, merge outputs with 2>&1, and discard unwanted data with /dev/null. Here documents and here strings let you send text directly from the shell into commands. With these tools, you can turn simple commands into reliable building blocks for more complex tasks.

Views: 64

Comments

Please login to add a comment.

Don't have an account? Register now!