UNIX / Linux Shell Input/output Redirections - Unix/Linux

What are UNIX / Linux Shell Input/ output Redirections?

In this episode, we will argue in point about the Shell input/output redirections. Most UNIX system commands take input from your terminal and send the resulting output back to your workstation. A command usually reads its input from the standard input, which happen to be your terminal by default. Correspondingly, a command generally writes its output to standard output, which is again your workstation by default.

Output Redirection

The output from a command usually projected for standard output can be easily diverted to a file instead. This ability is known as output redirection.

If the document > file is appended to any command that in general writes its output to standard output, the output of that command will be written to file in its place of your terminal.

Check the following who command which redirects the total output of the command in the users file.

Notice that no output appear at the workstation. This is because the output has been redirected from the default standard output mechanism (the terminal) into the specified file. You can check the users file for the complete substance –

If a command has its output redirected to a file and the file previously contains some data, that data will be lost. Consider the following instance –

You can use >> operator to attach the output in an existing file as follows –

Input Redirection

presently as the output of a command can be redirected to a file, so can the input of a command be redirected from a file. As the greater-than character > is used for output redirection, the less-than character < is used to redirect the input of a command.

The commands that as a rule take their input from the standard input can have their input redirected from a file in this method. For instance, to count the number of lines in the file users generate above, you can perform the command as follows –

Upon implementation, you will accept the following output. You can count the number of lines in the file by redirecting the standard input of the command from the file users –

Note that there is a difference in the output formed by the two forms of the we command. In the first case, the name of the file users is listed with the line count; in the second case, it is not.

In the first case, we know that it is reading its input from the file users. In the second case, it only knows that it is reading its input from standard input so it does not show file name.

Here Document

A here document is used to redirect input into an interactive shell script or program.

We can run an interactive program within a shell script without user action by supplying the required input for the interactive program, or interactive shell script.

The general form for a here document is –

Here the shell interprets the << operator as an instruction to read input until it find a line containing the specified delimiter. All the input lines up to the line containing the delimiter are then fed into the model input of the command.

The delimiter tells the shell that the here document has terminated. Without it, the shell continues to read the input forever. The delimiter must be a single word that does not include spaces or tabs.

Following is the input to the command we -l to count the total number of lines –

You can apply the here document to print many lines using your script as follows –

Upon execution, you will receive the following result –

The following script runs a session with the VI text editor and save the input in the file test.txt.

If you run this script with vim acting as VI, then you will likely observe output like the following –

After running the script, you should observe the following added to the file test.txt

Discard the output

Sometimes you will require executing a command, but you don't want the output display on the screen. In such cases, you can discard the output by redirecting it to the file /dev/null

Here command is the name of the command you want to perform. The file /dev/null is a particular file that manually rejects all its input.

To remove both output of a command and its error output, use standard redirection to redirect STDERR to STDOUT

Here 2 represent STDERR and 1 represents STDOUT. You can present a message on to STDERR by redirecting STDOUT into STDERR as follows –

Redirection Commands

Following is a total list of commands which you can use for redirection −

S.No. Command & Description
pgm > file
Output of pgm is redirected to file
pgm < file
Program pgm reads its input from file
pgm >> file
Output of pgm is appended to file
n > file
Output from stream with descriptornredirected to file
n >> file
Output from stream with descriptornappended to file
n >& m
Merges output from streamnwith streamm
n <& m
Merges input from streamnwith streamm
<< tag
Standard input comes from here through next tag at the start of line
Takes output from one program, or process, and sends it to another

Note that the file descriptor 0 is generally standard input (STDIN), 1 is standard output (STDOUT), and 2 is standard error output (STDERR).

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Unix/Linux Topics