Unix input redirection is a Linux operation that allows you to redirect where your standard input is coming from. Most Unix systems use the terminal as their input and output systems.
However, with the input redirection operation, you can change where your system gets its input to a file. Let’s take a closer input redirection in Linux with examples.
What Exactly is Unix Input Redirection?
Input redirection is an operation that allows you to specify that the command or input you intend to enter into the system should be gotten from a file. Input redirection is always redirected from a file, while output redirection in Linux is always redirected into a file.
In a normal Unix installation, you enter commands via the keyboard and get an output on your monitor. In other words, input is entered via the terminal by default, while output, also known as a stream, is normally directed or displayed on your computer monitor. Commands naturally get obtain their inputs from a source known as the standard input or “stdin,” and use the standard output or “stdout” as their destination or where to display their output.
With input redirection, commands that would normally obtain their input from your stdin source can take their input from a file instead. The less than sign “<” is used to denote input redirection, while the greater than sign “>” is used to denote output redirection.
– Working of Input Redirection
Let’s say you have a file named “users” with two different names:
$ cat users
Now, let’s assume you want to determine the number of lines in the file above by counting them, you’d normally write the command as:
$ wc -l users
As you can see, upon execution, you get the output above which shows you the number of users in the file. However, if you want to redirect the input, you’ll write the command like this:
$ wc -l < users
The output you’ll get will be different, as you can see above. While the first output of the wc command provided the name of the file users along with the line count, the second command with the i/o redirection doesn’t.
The reason is that the wc command knows that it’s taking its input from the users in the first case. But in the second case, the wc command can only understand that it’s taking its input from the stdin, which in turn makes it unable to provide the file name.
– Redirection Commands
There are many different redirection commands you can use for both output and input redirection. Some of them include:
- pgm > file: Redirects the output to a file
- pgm < file: Allows program to read input from file
- n > file: Redirects output from stream with the n descriptor to file
- pgm >> file: Appends the output of pgm to file
- n >> file: Output from the stream with the descriptor n is appended to file
- n <& m: Merges stream m with the input from stream n
- n >& m: Merges stream m with the output from stream n
- |: Takes the output from one process or program and sends it to a different program.
- <<tag: Standard input is gotten from here via the next tag at the beginning of the line
Types of Redirections
There are three main types of Unix output/input redirections. These types include overwrite, appends, and merge redirections. The here document and error redirection are not considered as main types, but they also serves a similar function.
The first type of redirection is overwrite. This redirection type overwrites whatever you have in a file, and it’s symbolized by “>” for stdout and “<” for stdin.
This redirection type allows you to append one file to the end of the other file. The standard output for this command is denoted as “>>” while “<<” is used to denote the standard input.
The merge command allows you to merge standard streams from one file with the other. It’s denoted by “>&” for merging outputs and “<&” for merging inputs. So for instance, “p >& q” means to merge the output coming from stream p with stream q, while “p <& q” means to merge the input coming from stream p with stream q.
– The Here Document
A here document allows you to redirect input into an interactive program or shell.
With a here document, you can run interactive programs inside a shell script without having to worry about user action, as all you have to do is supply the necessary input needed by the interactive shell script or the interactive program.
Here documents generally follow this form:
command << delimiter
With the form above, the << operator tells the shell to read input until it gets to a line with the set delimiter. The input lines that precede the line that contains the delimiter are considered to be the stdin of the command.
The function of the delimiter is to tell the shell where the here document stops. If there’s no delimiter, the shell will continue to read the input non-stop. Also, the delimiter needs to be a single word with no tabs or spaces. Below is an example of how a here document works:
$ wc -l << EOF
This program is a test
to understand how here documents work
The “EOF” from the example is the delimiter, and the command tells the program to count the total number of lines.
– Error Redirection in Linux
This redirection command allows you to transfer the errors generated due to false commands to a file instead of your standard output. Whenever you execute a program on your terminal, three different files are generated with different file descriptors.
The stdin is denoted by “0,” the stdout is denoted by “1,” and the standard error is denoted by “2.” An error stream is always displayed on the screen by default. You can use this redirection to redirect errors to a file instead of the screen.
Take this for instance:
$ arandomcommand 2>error.txt
This command uses “2” as the file descriptor, and it tells the program to redirect the error output to the error.txt file instead of displaying it on the standard output. If you then decide to write a different command and output the error.txt file with the current output, you can merge both together by writing the Linux redirection 2>&1. For instance:
$ ls directory ddd > error.txt 2>&1
With the merge command 2>&1 above, you’re redirecting the standard error to stdout, which means that the error message, which is “2” will be merged with your current output, which is “1.” If the directory specified is unavailable, the merged output will be redirected to error.txt instead.
That’s all on Unix input redirection command in Linux, what it is, and how it works.
Make sure to keep these few points in mind when redirecting standard input:
- Input redirection is denoted with the less than “<” symbol.
- Not adding a delimiter to your “Here document” will cause the program to continue reading input.
- Standard input uses “0” as its file descriptor, while standard output uses “1” as its file descriptor.
- Using “2” as your file descriptor signifies standard error, and you redirect it to a file as well.
Make sure to use the “<<” symbol to append and the “<&” to merge your inputs. Using “<” only will overwrite any input you have already, so if you intend to add or merge inputs, make sure to include the appropriate symbols.