I/O redirection is a very important function in linux/unix. It can change the standard input/output device when executing commands.
I/O redirection is a process that captures the output of a command, application, or script, and then sends the captured output as input to a file, command, or script.
Before introducing I/O redirection, let’s first understand a few important concepts:
File Descriptors (FD)
Standard input (stdin)
Standard output (stdout)
Standard error (stderr)
As long as we execute applications/commands on the terminal, we will always open 3 files（FD）, namely standard input, standard output, and standard error.
During our standard interaction with the terminal, standard input is transmitted through our keyboard. Standard output and standard error are displayed on our terminal as text.
File Descriptors (FD)
In Linux/Unix, everything is a file. Regular file, Directories, and even Devices are files. Every File has an associated number called File Descriptor (FD).
The standard input, standard output and standard error files are always exist when running the program.
Standard Input STDIN
Standard Output STDOUT
Standard Error STDERR
By default, standard error is displayed on the terminal. Error redirection routes errors to files outside the terminal.
The standard input stream usually transfers data from the user to the program. Programs that expect standard input usually receive input from devices such as keyboards. Terminate standard input by reaching EOF (end of file).
In the following example, we will demonstrate the execution of the cat command in the terminal. When we input content through the keyboard (standard input), standard output will output what we input.
➜ ~ cat
We can enter EOF by pressing ctrl-d. After the cat command receives EOF, it will stop.
Standard output can write command results or program-generated data, etc. When standard output is not redirected, it will output text to the terminal.
In the following example, we will use the echo command to write test content to standard output.
➜ ~ echo "write standard output"
write standard output
Standard error will be written to the error generated by the command, and the error indicates that the command failed to execute. As with standard output, the default target for this stream is terminal display.
After understanding the concept of redirection, let’s take a look at redirection symbols, which can help us change the standard input or output.
> standard output
< standard input
2> standard error
>> standard output
<< standard input
2>> standard error
In the following example, we will use echo command, overwrite redirection symbol (>) and append redirection symbol (>>) to show you that echo output is redirected to a file.
In the following example, we will use input redirection and pass the contents of the redirectionFile.log to the awk command through the input redirection symbol “<” as an input parameter and execute it.