0
0
Bash Scriptingscripting~20 mins

File descriptors and redirection in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Descriptor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
What is the output of this redirection command?
Consider the following bash command:

echo 'Hello' 1>&2

What will be the output when you run this command in a terminal?
Bash Scripting
echo 'Hello' 1>&2
AHello printed to standard error (screen)
BHello printed to standard output (screen)
CNo output, command runs silently
DSyntax error due to incorrect redirection
Attempts:
2 left
💡 Hint
Remember that 1 is standard output and 2 is standard error.
💻 Command Output
intermediate
1:30remaining
What happens when redirecting both stdout and stderr to a file?
Given this command:

ls /nonexistent /tmp > output.txt 2>&1

What will be the content of output.txt after running it?
Bash Scripting
ls /nonexistent /tmp > output.txt 2>&1
AOnly the listing of /tmp directory is saved in output.txt
BBoth the error message and the listing of /tmp are saved in output.txt
COnly the error message about /nonexistent is saved in output.txt
Doutput.txt remains empty
Attempts:
2 left
💡 Hint
Think about what 2>&1 means after redirecting stdout.
📝 Syntax
advanced
2:00remaining
Which command correctly appends stderr to a file without affecting stdout?
You want to append only the error messages (stderr) of a command to a file named errors.log, while keeping the standard output visible on the screen. Which command does this correctly?
Acommand >> errors.log 2>&1
Bcommand > errors.log 2>> errors.log
Ccommand 2> errors.log >> output.log
Dcommand 2>> errors.log
Attempts:
2 left
💡 Hint
Appending means using >> and only redirect stderr (2).
🔧 Debug
advanced
2:00remaining
Why does this redirection command produce unexpected output?
Look at this command:

command 2>&1 > output.txt

It produces unexpected output or error. Why?
ABecause output.txt does not exist
BBecause the order of redirections is correct and should not fail
CBecause stderr is redirected to stdout before stdout is redirected to the file
DBecause 2>&1 is invalid syntax
Attempts:
2 left
💡 Hint
Order of redirections matters in bash.
🚀 Application
expert
2:30remaining
How to redirect stdout to a file and stderr to another file simultaneously?
You want to run a command so that its standard output goes to out.log and its standard error goes to err.log. Which command achieves this correctly?
Acommand > out.log 2>&1 > err.log
Bcommand 2> err.log > out.log
Ccommand > out.log 2> err.log
Dcommand > out.log 2>&1
Attempts:
2 left
💡 Hint
Use separate redirections for stdout and stderr.