Are you looking for information related to the Linux input-output redirection? Then, read on. So, what’s redirection? Redirection is a Linux feature. With the help of it, you are able to change standard I/O devices. In Linux, when you enter a command as an input, you receive an output. It’s the basic workflow of Linux.
The standard input or stdin device to give commands is the keyboard and the standard output or stdout device is your terminal screen. With redirection, you can change the standard input/output. From this article, let’s find out how Linux input-output redirection works.
Standard Streams in Input-Output Redirection
The bash shell of Linux has three standard streams of input-output redirection, 1) Standard Input or Stdin, 2) Standard Output or Stdout, and 3) Standard Error or Stderr.
The standard input stream is denoted as
stdin (0). The bash shell receives input from
stdin. The keyboard is used to give input. The standard output stream is denoted as
stdout (1). The bash shell sends the output to stdout. The final output goes to the display screen. Here
2 are called file descriptors (FD). In the following section, we’ll look into file descriptors in detail.
In Linux, everything is a file. Directories, regular files, and even the devices are considered to be files. Each file has an associated number. This number is called File Descriptor or FD.
Interestingly, your terminal screen also has a definite File Descriptor. Whenever a particular program is executed, its output gets sent to your screen’s File Descriptor. Then, you can see the program output on the display screen. If the program output gets sent to your printer’s FD, the output would be printed.
2 are used as file descriptors for
stderr files respectively.
<’ sign is used for the input or stdin redirection. For example, Linux’s mail program sends emails from your Linux terminal.
You can type the email contents with the standard input device, keyboard. However, if you’re willing to attach a file to the email, use Linux’s input redirection feature. Below is a format to use the stdin redirection operator.
Mail -s "Subject" to-address < Filename
This would attach a file with your email, and then the email would be sent to a recipient.
>’ sign signifies the output redirection. Below is an example to help you understand its functions.
ls -al > listings
In the above example, the output command ls-al will be redirected to the file “listings” and it won’t go to your screen.
Note: In case, there is already a file present with the same name, the above command deletes that file contents, and the file gets overwritten. If you want a file not to be overwritten and you want to add content to the existing file, use the ‘
>>’ operator. Use the “
>&” operator to redirect the program output from one file to another.
You can not only redirect standard output to files, but also to the devices.
$ cat music.mp3 > /dev/audio
In the above instance, the cat command is used to read the file music.mp3. It sends the output to the audio device
/dev/audio. If you have the correct sound configurations on PC, the above command plays the music file music.mp3.
Error redirection is a very popular feature in Linux. Several times, you get massive amounts of errors while running your code. Error redirection means sending those errors to a particular file instead of the screen. By default, the error stream gets displayed on your screen.
When you search files, you often get permission denied errors. These errors don’t help in your search. When shell scripts are executed, it’s not desired to see error messages cluttering the normal output. As the solution, redirection of these errors is used.
$ myprogram 2>errorsfile
In the above example, the program named “myprogram” will be executed. “
2>” redirects the error output to the particular file named “errorfile”. Thus the program output doesn’t get cluttered with errors. You can also perform error redirection using its File Descriptor
Through this article, we learned about the streams of Linux input-output redirection. We discussed what is input redirection, what is output redirection, and what is error redirection. I hope you find this article useful.