Debug School

Cover image for I/O Redirection
Suyash Sambhare
Suyash Sambhare

Posted on

I/O Redirection

I/O redirection in Linux is a powerful feature that allows you to control where input and output of commands go. Here are the basics:

  1. Standard Input (stdin): This is the stream of data that is typically entered from the keyboard into a program. Its file descriptor is 0.
  2. Standard Output (stdout): Usually written to the terminal, this data stream is where a program writes its output. Its file descriptor is 1
  3. Standard Error (stderr): This is the data stream where a program writes its error messages, typically to the terminal. Its file descriptor is 2.

Types of Redirection

  1. Output Redirection:

    • >: Redirects stdout to a file, overwriting the file if it exists.
     ls > file.txt
    
  • >>: Redirects stdout to a file, appending to the file if it exists.

     ls >> file.txt
    
  1. Input Redirection:

    • <: Redirects stdin from a file.
     wc -l < file.txt
    
  2. Error Redirection:

    • 2>: Redirects stderr to a file.
     ls non_existent_file 2> error.log
    
  3. Combining Output and Error Redirection:

    • &>: Redirects both stdout and stderr to a file.
     command &> output_and_error.log
    
  4. Pipes:

    • |: Redirects stdout of one command to stdin of another.
     ls | grep "pattern"
    

These tools allow you to manage and manipulate the flow of data in your Linux environment efficiently

IO Redirection

I/O Redirection

Stdin (the keyboard), Stdout (the screen), and Stderr (error messages output to the screen) are the three default files that are always open. You can redirect this and any other open files. To put it simply, redirection is the process of taking output from one file, command, program, script, or even a block of code inside a script and sending it as input to another file, command, program, or script. A file descriptor is assigned to every open file. For stdin, stdout, and stderr, the corresponding file descriptors are 0, 1, and 2. The remaining descriptors are 3 to 9 for opening more files. Assigning one of these extra file descriptors to stdin, stdout, or stderr as a temporary duplicate link can occasionally be helpful. This makes it easier to return to normal following intricate rerouting and rearranging.
Data streams and peripherals (device files) are typically handled as files in UNIX and Linux, just like regular files. The operating system uses a file descriptor, which is only a number, to identify an open file. Think of it as a more straightforward kind of file pointer. It is comparable to a C file handle. Using file descriptor 5 could lead to issues. The child process inherits fd 5 when Bash creates one, as with exec. It's best to ignore this specific FD.

Closing File Descriptors

  • Close input file descriptor n. n<&-
  • Close stdin. 0<&-, <&-
  • Close output file descriptor n. n>&-
  • Close stdout. 1>&-, >&-

Child processes inherit open file descriptors. This is why pipes work. To prevent an fd from being inherited, close it.

# Redirecting only stderr to a pipe.
exec 3>&1                              # Save current "value" of stdout.
ls -l 2>&1 >&3 3>&- | grep bad 3>&-    # Close fd 3 for 'grep' (but not 'ls').
#              ^^^^   ^^^^
exec 3>&-                              # Now close it for the remainder of the script.
Enter fullscreen mode Exit fullscreen mode

Redirecting both stdout and stderr

To redirect both the standard output (stdout) and standard error (stderr) to different files in Linux, you can use the following syntax:

command > output.txt 2> error.txt

Here's a breakdown of what this does:

  • command: The command you want to run.
  • >: Redirects stdout to output.txt.
  • 2>: Redirects stderr to error.txt.

For instance, you could use ls /some/directory > output.txt 2> error.txt to list the contents of a directory and record any issues separately.
This will store any error messages to error.txt and the standard output of the ls command to output.txt.
Apart from the fundamental redirection choices we've covered, Linux offers a number of additional helpful redirection methods:

  1. Appending Output:

    • >>: Appends stdout to a file without overwriting it.
     echo "New line" >> file.txt
    
  2. Redirecting Both stdout and stderr:

    • &>: Redirects both stdout and stderr to the same file.
     command &> combined_output.txt
    
  3. Here Documents (Heredoc):

    • <<: Allows you to provide input to a command directly within the script.
     cat << EOF
     This is a
     multiline input
     EOF
    
  4. Here Strings:

    • <<<: Provides a string as input to a command.
     grep "pattern" <<< "This is a string"
    
  5. Merging stdout and stderr:

    • 2>&1: Redirects stderr to the same location as stdout.
     command > output.txt 2>&1
    
  6. Disabling Output:

    • /dev/null: Redirects output to a special file that discards all data written to it.
     command > /dev/null 2>&1
    

These options give you a lot of flexibility in managing how commands interact with files and other commands.

Ref: https://linuxhandbook.com/redirection-linux/
Ref: https://tldp.org/LDP/abs/html/io-redirection.html

Top comments (0)