Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

UNIX input/output redirection


May 23, 2021 UNIX Getting started


Table of contents


Input/output redirection

Most UNIX system commands accept input from your terminal and send the resulting output back to your terminal. A command usually reads input from a place called standard input, which by default happens to be your terminal. Similarly, a command typically writes its output to standard output, which by default is also your terminal.

Output redirection

The output of a command is typically used for standard output, or it can be easily transferred to a file. This capability is called output redirection:

If the > file these commands typically write their output to the standard output, and the output of the command is written to the file instead of your terminal:

Check the who command below, which redirects the full output of the command in the user file.

    $ who > users

Note that no output appears in the terminal. T his is because the output has been redirected from the default standard output device (terminal) to the specified file. If you want to check the users file, it has the full content:

    $ cat users
    oko tty01   Sep 12 07:30
    ai  tty15   Sep 12 13:32
    ruthtty21   Sep 12 10:10
    pat tty24   Sep 12 13:07
    steve   tty25   Sep 12 13:03
    $

If the command output is redirected to a file that already contains some data, the data will be lost. Consider this example:

    $ echo line 1 > users
    $ cat users
    line 1
    $

You >> the output to an existing file as follows:

    $ echo line 2 >> users
    $ cat users
    line 1
    line 2
    $

Enter the redirect

Just as the output of a command can be redirected to a file, the input of a command can be redirected from the file. Greater > for output redirection, less than the sign and < used to redirect the input of a command.

Commands that typically get input from standard input can have their own way of redirecting input from a file. For example, in order to calculate the number of rows in the file generated by the user above, you can perform the following command:

    $ wc -l users
    2 users
    $

Here, it produces an output of 2 rows. You can redirect the wc command by standard input from the user file:

    $ wc -l < users
    2
    $

Note that there is a difference between the output produced by the two forms of wc command. In the first case, the name of the user of the file is listed by the number of rows, and in the second case, it is not.

In the first case, wc knows that it is reading input from the file user. In the second case, only that it is read from the standard input, so it does not display the file name.

Here document

Here document is used to redirect input to an interactive shell script or program.

In a Shell script, we can run an interactive program without user action by providing the input required for the interactive program or interactive Shell script.

Here documents are generally in the form of:

    command << delimiter
    document
    delimiter

Here the shell << the input instruction until it finds a line containing the specified separator. All input lines that contain line separators are then fed into the standard input of the command.

The separator tells the shell here that the document is complete. W ithout it, the shell constantly reads the input. Separators must be a character and do not contain spaces or tabs.

Here's the total number of lines that you enter the command wc -l to calculate:

    $wc -l << EOF
        This is a simple lookup program 
        for good (and bad) restaurants
        in Cape Town.
    EOF
    3
    $

You can compile multiple lines with here document, and the script is as follows:

    #!/bin/sh

    cat << EOF
    This is a simple lookup program 
    for good (and bad) restaurants
    in Cape Town.
    EOF 

This results in the following:

    This is a simple lookup program
    for good (and bad) restaurants
    in Cape Town.

The following script runs a session with the vi text editor and saves the input file in the .txt file.

    #!/bin/sh

    filename=test.txt
    vi $filename <<EndOfCommands
    i
    This file was created automatically from
    a shell script
    ^[
    ZZ
    EndOfCommands

If you run the script with vim as vi, you are likely to see the following output:

    $ sh test.sh
    Vim: Warning: Input is not from a terminal
    $

After you run the script, you should see the following added to .txt test:

    $ cat test.txt
    This file was created automatically from
    a shell script
    $

Discard the output

Sometimes you need to execute commands, but you don't want to display the output on the screen. In this case, you can discard the output /dev/null

    $ command > /dev/null

Here command is the name of the command to be executed. File /dev/null special file that automatically discards all its inputs.

To discard the output of a command and its error output, you can use standard redirection to redirect STDOUT to STDERR:

    $ command > /dev/null 2>&1

Here, 2 stands for STDERR and 1 for STDOUT. You can display a message by redirecting the STDERR to the STDERR, as follows:

    $ echo message 1>&2

Redirect commands

The following is a complete list of commands that can be redirected:

Command Describe
pgm > file The output of the pgm is redirected to the file
Pgm The pgm program enters it from the file degree
pgm >> file The output of pgm is added to the file
n > file The output stream with the descriptor n is redirected to the file
n >> file The output stream with the descriptor n is added to the file
n >& m Merge the output of streams n and m
N Merge the inputs for streams n and m
The standard input starts with the next marker on the start line.
| Get input from one program or process and send it to another.

It is important to note that the file descriptor 0 is usually standard input (STDIN), 1 is standard output (STDOUT), and 2 is standard error output (STDERR).