Redirection

As mentioned above, there are always three default files open, stdin (the keyboard), stdout (the screen) and stderr (error messages output to the screen).

 

These, and any other open files, can be redirected.

 

The redirection operators are:

 

  • command > filename
    • Create file, if it doesn't already exist, and send output to it
    • If file does exist, its contents will be truncated (overwritten)
  • command >> filename
    • Append output to file
    • Create file, if it doesn't already exist
  • command < filename
    • Read input from file

 

Redirection simply means capturing output from a file, command, program, script, or even code block within a script and sending it as input to another file, command, program, or script.

 

Send output from previous shell script into text file:

 

Append output from previous shell script to text file

 

Read text file into cat command:

 

Similarly, redirection can be performed on:

  1. stdout to a file
  2. stderr to a file
  3. stdout to a stderr
  4. stderr to a stdout
  5. stderr and stdout to a file
  6. stderr and stdout to stdout
  7. stderr and stdout to stderr

 

 

 

The redirector operators can also be combined: