File Descriptor Image
Shell Process File Descriptors Link to original SHELL PROCESS
FD 0: stdin FD 1: stdout FD 2: stderr Keyboard (Terminal) <!-- File input --> <rect x="50" y="250" width="80" height="40" rx="5" fill="#f39c12" stroke="#e67e22" stroke-width="2"/> <text x="90" y="265" text-anchor="middle" font-family="Arial, sans-serif" font-size="11" fill="white">File</text> <text x="90" y="280" text-anchor="middle" font-family="monospace" font-size="9" fill="white">< input.txt</text> <!-- Pipe input --> <rect x="50" y="310" width="80" height="40" rx="5" fill="#9b59b6" stroke="#8e44ad" stroke-width="2"/> <text x="90" y="325" text-anchor="middle" font-family="Arial, sans-serif" font-size="11" fill="white">Pipe</text> <text x="90" y="340" text-anchor="middle" font-family="monospace" font-size="9" fill="white">cmd1 | cmd2</text>Terminal Display <!-- File output --> <rect x="670" y="220" width="80" height="40" rx="5" fill="#f39c12" stroke="#e67e22" stroke-width="2"/> <text x="710" y="235" text-anchor="middle" font-family="Arial, sans-serif" font-size="11" fill="white">File</text> <text x="710" y="250" text-anchor="middle" font-family="monospace" font-size="9" fill="white">> output.txt</text> <!-- Pipe output --> <rect x="670" y="280" width="80" height="40" rx="5" fill="#9b59b6" stroke="#8e44ad" stroke-width="2"/> <text x="710" y="295" text-anchor="middle" font-family="Arial, sans-serif" font-size="11" fill="white">Next Cmd</text> <text x="710" y="310" text-anchor="middle" font-family="monospace" font-size="9" fill="white">cmd1 | cmd2</text>Terminal (Errors) <!-- Error file --> <rect x="670" y="420" width="80" height="40" rx="5" fill="#e67e22" stroke="#d35400" stroke-width="2"/> <text x="710" y="435" text-anchor="middle" font-family="Arial, sans-serif" font-size="11" fill="white">Error File</text> <text x="710" y="450" text-anchor="middle" font-family="monospace" font-size="9" fill="white">2> errors.txt</text> <!-- /dev/null --> <rect x="670" y="480" width="80" height="40" rx="5" fill="#34495e" stroke="#2c3e50" stroke-width="2"/> <text x="710" y="495" text-anchor="middle" font-family="Arial, sans-serif" font-size="11" fill="white">/dev/null</text> <text x="710" y="510" text-anchor="middle" font-family="monospace" font-size="9" fill="white">2> /dev/null</text>
FD 0 FD 1 FD 2 Common Redirection Examples: echo "hello" > file.txt # stdout to file ls /bad 2> errors.txt # stderr to file sort < input.txt # stdin from file cmd 2> /dev/null # discard errors File Descriptor Numbers: 0 = stdin (input) 1 = stdout (normal output) 2 = stderr (error output) Always available in every process
Fundamentals
File Descriptors - Fundamentals
What are File Descriptors?
File descriptors are non-negative integers that represent open files or input/output channels in Unix-like systems. Every process starts with three standard file descriptors:
- 0 = stdin (standard input)
- 1 = stdout (standard output)
- 2 = stderr (standard error)
Basic Redirection
# Redirect stdout to a file echo "Hello" > output.txt # Same as: echo "Hello" 1> output.txt # Redirect stdin from a file cat < input.txt # Same as: cat 0< input.txt # Redirect stderr to a file ls /nonexistent 2> errors.txt # Append to a file instead of overwriting echo "More text" >> output.txtTesting File Descriptors
Use the
-ttest to check if a file descriptor is connected to a terminal:# Check if stdout is a terminal if [ -t 1 ]; then echo "Output is going to terminal" else echo "Output is redirected/piped" fi # Check stdin [ -t 0 ] && echo "Input is from terminal" # Check stderr [ -t 2 ] && echo "Errors go to terminal"More on this in output-destination-detection
Link to original