User Tools

Site Tools


bash:redirection

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
bash:redirection [2019/12/14 19:40] peterbash:redirection [2021/01/13 21:51] (current) – [BASH - Redirection] peter
Line 1: Line 1:
 ====== BASH - Redirection ====== ====== BASH - Redirection ======
  
-Each file in Linux has a corresponding File Descriptor associated with it.+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). 
 + 
 +  * Your screen has a File Descriptor.  When a program is executed the output is sent to File Descriptor of the screen, and you see program output on your monitor. 
 +  * Your printer has a File Descriptor.  If the output is sent to File Descriptor of the printer, the program output would be printed. 
 + 
 +---- 
 + 
 +===== Standard File Descriptors ===== 
 + 
 +Whenever you execute a program/command at the terminal, 3 files are always open, viz., standard input, standard output, standard error.   
 + 
 +These files are always present whenever a program is run. 
 + 
 + 
 +^File^File Descriptor^ 
 +|Standard Input STDIN|0| 
 +|Standard Output STDOUT|1| 
 +|Standard Error STDERR|2| 
  
 The keyboard is the standard input device while your screen is the standard output device. The keyboard is the standard input device while your screen is the standard output device.
  
-Summary:+However, files can have their input and output redirected, using:
  
-  * ">" is the output redirection operator. ">>" appends output to an existing file+  * ">" is the output redirection operator. 
 +  * ">>" appends output to an existing file
   * "<" is the input redirection operator   * "<" is the input redirection operator
   * ">&" re-directs output of one file to another.   * ">&" re-directs output of one file to another.
   * Errors can be re-directed using its corresponding File Descriptor 2.   * Errors can be re-directed using its corresponding File Descriptor 2.
- 
  
 ---- ----
Line 51: Line 71:
 ===== Input redirection ===== ===== Input redirection =====
  
-The '<' symbol is used for input(STDIN) redirection.+The '<' symbol is used for input (STDIN) redirection.
  
 <code bash> <code bash>
Line 61: Line 81:
 ---- ----
  
-===== File Descriptors (FD) ===== +===== Error Redirection =====
- +
-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). +
- +
-Your screen also has a File Descriptor. +
- +
-When a program is executed the output is sent to File Descriptor of the screen, and you see program output on your monitor. +
- +
-If the output is sent to File Descriptor of the printer, the program output would have been printed. +
- +
----- +
- +
-==== Error Redirection ==== +
- +
-Whenever you execute a program/command at the terminal, 3 files are always open, viz., standard input, standard output, standard error. +
- +
----- +
- +
-These files are always present whenever a program is run. +
- +
-As explained before a file descriptor, is associated with each of these files: +
- +
-^File^File Descriptor^ +
-|Standard Input STDIN|0| +
-|Standard Output STDOUT|1| +
-|Standard Error STDERR|2| +
- +
-By default, error stream is displayed on the screen. +
- +
-Error redirection is routing the errors to a file other than the screen. +
- +
-==== Why Error Redirection? ====+
  
 Error re-direction is one of the very popular features of Unix/Linux. Error re-direction is one of the very popular features of Unix/Linux.
Line 107: Line 92:
 The solution is to re-direct the error messages to a file. The solution is to re-direct the error messages to a file.
  
-=== Example 1 ===+==== Example 1 ====
  
 <code bash> <code bash>
Line 123: Line 108:
 ---- ----
  
-=== Example 2 === +==== Example 2 ====
- +
-Here is another example which uses find statement -+
  
 <code bash> <code bash>
bash/redirection.1576352401.txt.gz · Last modified: 2020/07/15 10:30 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki