File descriptors and redirection in Bash Scripting - Time & Space Complexity
When working with file descriptors and redirection in bash, it is important to understand how the script's execution time changes as it handles more input or output data.
We want to know how the time to run commands grows when redirecting or managing files.
Analyze the time complexity of the following bash script snippet.
#!/bin/bash
while read line; do
echo "$line" >&2
done < input.txt
cat input.txt > output.txt
This script reads each line from input.txt, writes it to standard error, then copies the entire file to output.txt.
Look for loops or repeated commands that run multiple times.
- Primary operation: The
while readloop reads each line once. - How many times: It runs once per line in
input.txt, so n times if there are n lines. - Secondary operation: The
catcommand reads the whole file once to copy it.
As the number of lines in input.txt grows, the loop runs more times, and the cat command processes a larger file.
| Input Size (n lines) | Approx. Operations |
|---|---|
| 10 | ~10 loop reads + 1 full file read |
| 100 | ~100 loop reads + 1 full file read |
| 1000 | ~1000 loop reads + 1 full file read |
Pattern observation: The total work grows roughly in proportion to the number of lines, because each line is read once and the whole file is copied once.
Time Complexity: O(n)
This means the time to run the script grows linearly with the number of lines in the input file.
[X] Wrong: "Redirection and file descriptor operations happen instantly and do not affect time complexity."
[OK] Correct: Even though redirection looks simple, reading and writing data through file descriptors takes time proportional to the data size, so it affects how long the script runs.
Understanding how file reading and writing scale helps you write efficient scripts and explain their performance clearly in real situations.
What if we replaced the while read loop with a command that reads the entire file at once? How would the time complexity change?
Practice
Solution
Step 1: Recall standard file descriptors
Standard input is 0, standard output is 1, and standard error is 2.Step 2: Identify the error stream descriptor
The error stream is always file descriptor 2 in bash.Final Answer:
2 -> Option CQuick Check:
Standard error = 2 [OK]
- Confusing standard output (1) with error (2)
- Thinking input (0) is error
- Using non-standard descriptor numbers
output.txt in bash?Solution
Step 1: Understand output redirection syntax
Using > redirects standard output (file descriptor 1) to a file, overwriting it.Step 2: Match syntax to redirect output only
command > output.txt uses > correctly to redirect output to output.txt.Final Answer:
command > output.txt -> Option DQuick Check:
Output redirection uses > [OK]
- Using < instead of > for output
- Redirecting error (2>) instead of output
- Appending (>>) when overwrite is needed
file.txt does not exist?cat file.txt > output.txt 2>&1Solution
Step 1: Understand redirection 2>&1
This redirects standard error (2) to the same place as standard output (1).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.Final Answer:
Redirects both output and error messages to output.txt -> Option AQuick Check:
2>&1 merges error into output [OK]
- Thinking error shows on terminal
- Assuming output.txt stays empty
- Confusing 2>&1 with separate redirections
error.log:ls -l > error.log 2>Solution
Step 1: Check error redirection syntax
Redirection 2> must be followed by a filename to redirect standard error.Step 2: Identify missing filename
The command ends with 2> but no file is specified, causing a syntax error.Final Answer:
Missing filename after 2> for error redirection -> Option BQuick Check:
2> needs a target file [OK]
- Leaving out filename after 2>
- Confusing 2> with 2>&1
- Thinking > needs to be 1>
output.log and error messages to error.log. Which command correctly achieves this?Solution
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.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.Final Answer:
./script.sh > output.log 2> error.log -> Option AQuick Check:
Separate > and 2> for output and error [OK]
- Using &> redirects both to one file
- Mixing order of redirections incorrectly
- Omitting file names after redirection operators
Ready to prove it?
Pass the test and this pattern moves unknown
