Skip to content
Home » Bash Redirection In Depth

Bash Redirection In Depth

Standard Output Redirection (“>”)

  • This section deals with redirecting the standard output of a command to a file or another command.
  • For example, the command ls -l > file.txt will save the output of the ls -l command to a file named “file.txt”

Standard Error Redirection (“>&”)

  • This section covers redirecting the standard error of a command to a file or another command.
  • For example, the command cat file.txt >& error.txt will save any error messages that occur when trying to read “file.txt” to a file named “error.txt”

Appending Output ( “>>”)

  • This section covers appending the standard output of a command to an existing file rather than overwriting it.
  • For example, the command echo "new text" >> file.txt will add the string “new text” to the end of the file “file.txt”

Piping Output ( “|”)

  • This section covers sending the output of one command as the input to another command.
  • For example, the command ls -l | grep "file" will search for the string “file” in the output of the ls -l command.

Input Redirection (“<“)

  • This section covers redirecting input for a command from a file.
  • For example, the command sort < input.txt will sort the contents of “input.txt”

/dev/null Redirection

  • This section covers discarding the output of a command by redirecting it to the special file “/dev/null”
  • For example, the command command > /dev/null will run the command but discard its output

Here Document

  • This section covers redirecting input from a string or a text block.
  • For example, the command command << EOF will run the command with input from the text block that follows, up to the marker EOF

Here String

  • This section covers redirecting input from a single string.
  • For example, the command command <<< "string" will run the command with input from the string “string”

Fd redirection

  • This section covers redirecting input and output for a command using file descriptor numbers.
  • For example, the command command 2>&1 will redirect the standard error to standard output.

Leave a Reply

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

one × 1 =