Loading...
Shell Script

Redirectors

The shell and many UNIX commands take their input from standard input (STDIN), write output to standard output (STDOUT), and write error output to standard error (STDERR). By default , standard input is connected to the terminal keyboard and standard output and error to the terminal screen.

Redirection of I/O to a file, is accomplished by specifying the destination on the command line using a redirection on the command line using a redirection metacharacters followed by the desired destination .

The BASH uses a format for redirection which includes numbers. The numbers refer to the file descriptor numbers (0 standard input, 1 standard output, 2 standard error).

Character                                    Action 
>                       Redirect standard output
2>                      Redirect standard error
2>&1                   Redirect standard error to standard output
<                       Redirect standard input
|                        Pipe standard output to another command
>>                     Append to standard output
2>&1|                 Pipe standard output and standard error to another command

Given below are the examples of Redirectors which are described above.

Example                       Detail
$ls >list.out                  Redirects the output of ls command to a file list.out
                                    Note: If there is a file  list.out this particular command removes the old data                                          and add the out of the latest command.
$lss 2>list.err              Redirects the STDERR  to list.err file. Whereas the normal output will be                                               printed on the screen.
$ls 2>$1  >list.out       Redirects both STDOUT and STDERR to list.out file 
$ls >>list.out               Appends out of ls command to list.out file. It will append the latest data to the                                        file and wont erases the earlier data.

Leave a Reply

Your email address will not be published. Required fields are marked *