0
0
Bash Scriptingscripting~10 mins

File descriptors and redirection in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - File descriptors and redirection
Start command
Open file descriptors
Redirect input/output
Execute command with redirected streams
Close file descriptors
End
This flow shows how a shell command opens file descriptors, redirects input/output, runs, then closes descriptors.
Execution Sample
Bash Scripting
echo "Hello" > output.txt
cat < output.txt
ls 2> error.log
This script writes 'Hello' to a file, reads it back, and redirects errors to a log file.
Execution Table
StepCommandFile Descriptor ActionResulting OutputNotes
1echo "Hello" > output.txtRedirect stdout (fd 1) to output.txt (create/overwrite)File output.txt contains 'Hello\n'Standard output goes to file, not screen
2cat < output.txtRedirect stdin (fd 0) from output.txtPrints 'Hello' to screenStandard input comes from file
3ls 2> error.logRedirect stderr (fd 2) to error.log (create/overwrite)Directory listing on screen (stdout)Errors go to file error.log
4EndClose all redirected file descriptorsNormal shell prompt resumesRedirections end after command
💡 All commands executed with proper redirections; file descriptors closed after each command
Variable Tracker
File DescriptorInitialAfter Step 1After Step 2After Step 3Final
stdin (fd 0)Terminal inputTerminal inputFrom output.txtTerminal inputTerminal input
stdout (fd 1)Terminal outputTo output.txtTerminal outputTerminal outputTerminal output
stderr (fd 2)Terminal error outputTerminal error outputTerminal error outputTo error.logTerminal error output
Key Moments - 3 Insights
Why does 'echo "Hello" > output.txt' not print anything on the screen?
Because stdout (file descriptor 1) is redirected to the file output.txt, so the output goes into the file instead of the terminal. See execution_table row 1.
How does 'cat < output.txt' get its input from the file?
The stdin (file descriptor 0) is redirected from output.txt, so cat reads from the file instead of the keyboard. See execution_table row 2.
What happens to error messages when using 'ls 2> error.log'?
The stderr (file descriptor 2) is redirected to error.log, so any error messages go into that file instead of the terminal. See execution_table row 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the destination of stdout after step 1?
Aoutput.txt file
BTerminal screen
Cerror.log file
DNo output
💡 Hint
Check the 'File Descriptor Action' column in row 1 of execution_table.
At which step does stdin get redirected from a file?
AStep 3
BStep 1
CStep 2
DNo redirection of stdin
💡 Hint
Look at the 'File Descriptor Action' column for stdin redirection.
If we remove '2> error.log' from step 3, what changes in the variable_tracker for stderr?
Astderr remains redirected to error.log
Bstderr outputs to terminal error output
Cstderr is redirected to output.txt
Dstderr is closed
💡 Hint
Check stderr changes in variable_tracker after step 3.
Concept Snapshot
File descriptors 0,1,2 are stdin, stdout, stderr.
Use > to redirect stdout, < for stdin, 2> for stderr.
Redirection changes where input/output go.
Commands run with redirected streams.
After command, descriptors close and shell returns to normal.
Full Transcript
This lesson shows how bash uses file descriptors 0,1,2 for input, output, and errors. Commands can redirect these streams to files or other sources. For example, 'echo "Hello" > output.txt' sends output to a file instead of the screen. 'cat < output.txt' reads input from that file. 'ls 2> error.log' sends error messages to a file. Each command opens the needed file descriptors, redirects them, runs, then closes them. This lets you control where data flows in scripts and commands.