0
0
Bash Scriptingscripting~5 mins

File descriptors and redirection in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a file descriptor in bash scripting?
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
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
How do you append output to a file instead of overwriting it?
A>
B<
C2>
D>>
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
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
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.