The standard input or STDIN is the command that we type in our terminal to interact with the Linux kernel.
In the above example, “ls -la” is the standard input or STDIN.
The standard output or STDOUT is the output of a running process or command given to the Linux kernel through standard input and is displayed on the terminal. For example, given above, the output be like
-rwxr-x--- 1 Ubuntu Ubuntu 89 Jan 4 2021 arith.sh
-rw-r--r-- 1 Ubuntu Ubuntu 3771 Jun 3 2020 .bashrc
drwx------ 28 Ubuntu Ubuntu 4096 Aug 1 13:10 .cache
drwxr-xr-x 36 Ubuntu Ubuntu 4096 Jul 29 18:30 .config
drwx------ 3 Ubuntu Ubuntu 4096 Nov 18 2020 .dbus
drwxr-xr-x 5 Ubuntu Ubuntu 4096 Jul 30 16:28 Desktop
drwxr-xr-x 13 Ubuntu Ubuntu 12288 Jul 28 19:53 Downloads
Every output has a pre-defined default place to go in the Unix-based operating systems. Some of the methods for redirecting output are given below.
- Standard output redirection “>”
- Standard Input redirection “<”
- Standard error redirection “2>”
- Standard output and error redirection “&>”
- Standard output redirection “>>”
- Standard input redirection “<<”
- Redirection using pipes “|”
Standard Output Redirection “ > “
Output redirection is a method in which the standard output of a command can be redirected to files or as standard input for another command. The “>” sign is used for output redirection. The terminal does not show the output; instead, it is written to a file or redirected as input to another command. For example
Instead of the output shown on the terminal, it will be redirected to the file.txt or pakegsNames file. In order to confirm whether the output was redirected or not, use the cat command to read the files.
Ubuntu@Ubuntu:~$ cat pakegName
This “ 1> ” operator is also used for redirecting standard output.
Standard Input Redirection “ < ”
Input redirection is a method in which the standard input of a command is redirected from a file or a standard output of another command. The “<” sign is used for input redirection. It is mostly the default action of a command. For example
Is the same as this.
It takes input from the file /etc/passwd instead of a keyboard. The “0<” sign can also be used for redirecting standard input.
Standard Error Redirection “2>“
With the redirection method, the standard errors can be redirected and written to a file. For example
If any error occurs, it will not show on the terminal window; rather, it will be stored in an error file. If the error file already exists, then it will be overwritten.
Standard Output and Standard Error Redirection “ &> “
With the ” &> ” sign, there is a more efficient way to redirect standard output and standard error simultaneously with the ” &>” sign. For example
All the package names and errors will be stored in the file pkgnames.
Standard Output Redirection “ >> ”
This redirection method redirects the standard output of a command or a file to another file. The difference is while using “>>” redirection, If the file already exists, the data will be appended to the file; hence the file will not be overwritten.
If the file pkgnames already exist, its content won’t be overwritten; rather, the output will be added at the end of the file.
Standard Input Redirection “ << ”
This redirection method reads the user input from the terminal and then appends it to the file.
If the file pkgnames already exist, then its content won’t be overwritten.
Redirection using Pipes “ | “
Pipes are often used when you want to combine multiple commands. With pipes, the output of the first command can be used as the input of the second command. For example
In the above command, the standard output of the command left to the pipe “|” sign is the standard input of the command right to the pipe “|” sign.
Combining the redirection operators
You can combine some of the redirection operators because they are conveniently easy to use and take less time; some of the combined redirection operators are given below.
|2>>:||This operator is used for redirecting standard errors to the file.|
|<>:||This operator is used for specified files as both standard input and standard output.|
|>&:||This operator redirects the output of one file to another.|
|<&:||This operator redirects the input of one file to another.|
|2>&1:||This operator is used for redirecting standard error to standard output.|
|1>&2:||This operator is used for redirecting standard output to standard error.|
As a Linux administrator, input-output redirection is a very common routine in daily work. Storing the output and errors for later use and combining the multiple commands make your work easier and reduce time so that you can work diligently and efficiently. Above are some tips to do so that will help you to understand input-output redirections.