How to use BASH RE-DIRECTIONS: Input, Output and Error

close up photo of programming of codes
Reading Time: 3 minutes

The execution of any command in Linux can result in the input requirements from the user, or displaying an output or both. The work of bash re-directions is to redirect the inputs and outputs to/from a file in Linux.

Need for bash Re-directions

Generally, we pass input to the script via the command line. It is also possible to pass these commands through a file. The output of the commands is either displayed on the screen straight away or is stored in another file as well. Linux gives us the flexibility to insert the inputs or export the outputs to another file. It is possible because of the re-directions of the values. 

Types of re-directions

  1. Input Redirections
  2. Output Redirections

Input Redirections:

  • Stdin:

Standard input is a facility to store and execute the commands in the script.  The bash shell takes input from stdin. The redirection of the input is done via less-than character (<). 
The commands usually take their input from the keyboard. The file descriptor for stdin is 0.


./ <name.txt

In the above example, name.txt file drives the input to the script.

Output Redirections: 

  • Stdout:
    After the execution of the command, the output then redirects to another file. It is stored and then further manipulations can be performed. The bash shell sends the output to stdout. Greater-than (>) character redirects the output of a command. The file descriptor for stdout is 1.


ls -l >list.txt

  • Stderr:
    It takes the errors as input from the output of a file. Its default file descriptor is 2. In the terminal, the error gets displayed on the user’s screen. 


grep da * 2>grep-errors.txt

It will write the errors to the file called grep-errors.txt.

RedirectionsFile Descriptor NumberDefinitionOverwriteAppend
STDIN0Standard Input <<<
STDOUT1Standard Output>>>
STDERR2Standard Error>&>&

Streams are similar to files in Linux. Reading from a file or writing something to a file requires a stream of data. When a program is running on the system, the kernel creates a table of file descriptors for the program to use. A file descriptor access a file or any other input/output resource. 
The Kernel maintains the file descriptor. Initially, all the descriptors point to a terminal device such that the keyboard provides the standard output and the output directs to the user’s screen. Redirection manipulates the file descriptors such that the inputs/outputs can be redirected as required.
The path of file descriptors is:

File descriptors in Unix can be passed between processes across Unix domain sockets using the sendmsg() system call.  
The re-directions and pipelines can actually be chained together to create a complex command. These commands run one after the other in the following manner.


sort -myfile.txt | uniq >newfile.txt

File descriptors have great functionality and more operations continue to add to the modern Unix-like systems. The new operations are added to defend against Time of Check to Time of Use types of software bugs that are caused by the race conditions.


Written by 

Vidushi Bansal is a Software Consultant [Devops] at Knoldus Inc. She is passionate about learning and exploring new technologies.