Bird
Raised Fist0
Bash Scriptingscripting~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which file descriptor number represents the standard error stream in bash?
easy
A. 3
B. 1
C. 2
D. 0

Solution

  1. Step 1: Recall standard file descriptors

    Standard input is 0, standard output is 1, and standard error is 2.
  2. Step 2: Identify the error stream descriptor

    The error stream is always file descriptor 2 in bash.
  3. Final Answer:

    2 -> Option C
  4. Quick Check:

    Standard error = 2 [OK]
Hint: Remember: 0=input, 1=output, 2=error [OK]
Common Mistakes:
  • Confusing standard output (1) with error (2)
  • Thinking input (0) is error
  • Using non-standard descriptor numbers
2. Which of the following is the correct syntax to redirect standard output to a file named output.txt in bash?
easy
A. command >> output.txt 2>&1
B. command < output.txt
C. command 2> output.txt
D. command > output.txt

Solution

  1. Step 1: Understand output redirection syntax

    Using > redirects standard output (file descriptor 1) to a file, overwriting it.
  2. Step 2: Match syntax to redirect output only

    command > output.txt uses > correctly to redirect output to output.txt.
  3. Final Answer:

    command > output.txt -> Option D
  4. Quick Check:

    Output redirection uses > [OK]
Hint: Use > for output, < for input redirection [OK]
Common Mistakes:
  • Using < instead of > for output
  • Redirecting error (2>) instead of output
  • Appending (>>) when overwrite is needed
3. What will be the output of the following command if file.txt does not exist?

cat file.txt > output.txt 2>&1
medium
A. Redirects both output and error messages to output.txt
B. Creates an empty output.txt file with no error message
C. Outputs the error message to the terminal and creates output.txt
D. Throws a syntax error and does nothing

Solution

  1. Step 1: Understand redirection 2>&1

    This redirects standard error (2) to the same place as standard output (1).
  2. Step 2: Analyze command behavior when file missing

    cat tries to read file.txt, fails, and sends error to stderr, which is redirected to output.txt along with stdout.
  3. Final Answer:

    Redirects both output and error messages to output.txt -> Option A
  4. Quick Check:

    2>&1 merges error into output [OK]
Hint: 2>&1 sends errors to the same place as output [OK]
Common Mistakes:
  • Thinking error shows on terminal
  • Assuming output.txt stays empty
  • Confusing 2>&1 with separate redirections
4. Identify the error in this command that attempts to redirect only standard error to error.log:

ls -l > error.log 2>
medium
A. Output redirection > should be 1>
B. Missing filename after 2> for error redirection
C. Should use 2>&1 instead of 2>
D. Command syntax is correct

Solution

  1. Step 1: Check error redirection syntax

    Redirection 2> must be followed by a filename to redirect standard error.
  2. Step 2: Identify missing filename

    The command ends with 2> but no file is specified, causing a syntax error.
  3. Final Answer:

    Missing filename after 2> for error redirection -> Option B
  4. Quick Check:

    2> needs a target file [OK]
Hint: Always specify a file after 2> for error redirection [OK]
Common Mistakes:
  • Leaving out filename after 2>
  • Confusing 2> with 2>&1
  • Thinking > needs to be 1>
5. You want to run a script that outputs normal messages to output.log and error messages to error.log. Which command correctly achieves this?
hard
A. ./script.sh > output.log 2> error.log
B. ./script.sh 2>&1 > output.log error.log
C. ./script.sh > output.log 2>&1 error.log
D. ./script.sh > output.log &> error.log

Solution

  1. Step 1: Understand separate redirection for output and error

    To send stdout and stderr to different files, use > for stdout and 2> for stderr separately.
  2. Step 2: Analyze each option

    ./script.sh > output.log 2> error.log correctly redirects stdout to output.log and stderr to error.log. Others mix or misuse redirection syntax.
  3. Final Answer:

    ./script.sh > output.log 2> error.log -> Option A
  4. Quick Check:

    Separate > and 2> for output and error [OK]
Hint: Use > for output, 2> for errors to separate files [OK]
Common Mistakes:
  • Using &> redirects both to one file
  • Mixing order of redirections incorrectly
  • Omitting file names after redirection operators
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard