Redirection in Bash

In Bash, redirection is a way to control the input and output of commands. It allows you to redirect the standard input, standard output, and standard error of commands to different sources or destinations. Here are some common redirection operators:

  1. Standard Output (STDOUT) redirection:
    • >: Redirects the standard output of a command to a file. If the file exists, it gets overwritten. Example: command > output.txt
    • >>: Appends the standard output of a command to a file. If the file doesn’t exist, it gets created. Example: command >> output.txt
  2. Standard Error (STDERR) redirection:
    • 2>: Redirects the standard error of a command to a file. Example: command 2> error.txt
    • 2>>: Appends the standard error of a command to a file. Example: command 2>> error.txt
  3. Combining STDOUT and STDERR:
    • &> or &>>: Redirects both standard output and standard error of a command to a file. Example: command &> output_error.txt or command &>> output_error.txt
  4. Standard Input (STDIN) redirection:
    • <: Redirects the standard input of a command from a file. Example: command < input.txt
  5. Here documents:
    • <<: Allows you to provide input to a command inline without using a separate file. Example:

command << EOF
line 1
line 2
EOF

6. Pipes:

These are some of the basic redirection operators in Bash. They can be combined and used in various ways to control the input and output of commands, allowing you to manipulate data and automate tasks effectively.

Leave a Comment

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