Piping & Output Redirection in Bash

,

These are my rough notes learning Linux through a module in Pwn College’s Linux Luminarium dojo.

So, every process in Linux has three different standard channels of communication:
stdin Short for Standard Input is the channel used by the process to take input just like echo takes input from the user to display text
stdout Aka Standard Output is used to get output from the process like printing contents of a file on the terminal through cat
stderr Also called Standard Error outputs errors generated by the process in question
> The greater than symbol would redirect output of a command to a file. This would overwrite contents of a file if it exists already
>> This is similar to > but instead of rewriting the contents of a file this would append the output of a command inside the file if it exists already
< The less than symbol would redirect input back to the command

File Descriptors are numbers that describe the communication channel in a linux process. We are already familiar with the three communication channels:

  • FD: 0 – Standard Input
  • FD: 1 – Standard Output
  • FD: 2 – Standard Error

The output from using a File Descriptor could be redirected as such:
cat file 2> log
Here the file log would have output of the stderr for file redirected to it.

  • | is the pipe operator in bash. Piping is redirection of a command’s output.
    • Piping works from left to right which means stdout of the command on the right side of the pipe would have its output redirected to the stdin of the command to the right side of the pipe. The pipe symbol can only redirect stdout.
  • >& operator redirects a file descriptor to another file descriptor.
  • tee command takes output of a command and further outputs it onto the screen and also writes that to any file that is mentioned as its arguments
  • Process Substitution – It is a way to use a command’s stdin or stdout as if it were a file. During process substitution a temporary file is mapped as input or output for that command. Process substitution can be performed by enclosing a command in braces of the process substitution operator >() or <().