A file descriptor is a number that represents an open file or input/output resource in bash. Common ones are 0 for standard input, 1 for standard output, and 2 for standard error.
Click to reveal answer
beginner
How do you redirect standard output to a file in bash?
Use the > symbol followed by the filename. For example, echo "Hello" > file.txt sends the output to file.txt instead of the screen.
Click to reveal answer
intermediate
What does the command 2>&1 do in bash?
It redirects standard error (file descriptor 2) to the same place as standard output (file descriptor 1). This way, both outputs go to the same destination.
Click to reveal answer
beginner
Explain the difference between > and >> in bash redirection.
> overwrites the file with new output, while >> appends the output to the end of the file without deleting existing content.
Click to reveal answer
intermediate
How can you redirect only standard error to a file, leaving standard output unchanged?
Use 2> filename. For example, command 2> error.log sends error messages to error.log but shows normal output on the screen.
Click to reveal answer
Which file descriptor number represents standard input in bash?
A0
B1
C2
D3
✗ Incorrect
Standard input is always file descriptor 0.
What does the command ls > files.txt do?
AAppends the list of files to files.txt
BSends the list of files to the screen
CSends the list of files to the file files.txt
DDeletes files.txt
✗ Incorrect
The > symbol redirects output to the file, overwriting it.
How do you append output to a file instead of overwriting it?
A>
B<
C2>
D>>
✗ Incorrect
The >> operator appends output to the file.
What does command 2>&1 do?
ARedirects standard output to standard error
BRedirects standard error to standard output
CRedirects both to a file
DRuns command twice
✗ Incorrect
It sends standard error (2) to the same place as standard output (1).
How to redirect only error messages to a file named error.log?
Acommand 2> error.log
Bcommand >> error.log
Ccommand > error.log
Dcommand 1> error.log
✗ Incorrect
2> redirects only standard error to the file.
Describe what file descriptors 0, 1, and 2 represent in bash and how you can redirect each.
Think about where data comes from and goes to in commands.
You got /6 concepts.
Explain how to combine standard output and standard error into one file using redirection.
Remember the order matters in redirection.
You got /3 concepts.
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
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 C
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
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 D
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
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 A
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
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 B
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
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 A
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 unknownstrongwhile earning +1 Grip toward your certificate.