Challenge - 5 Problems
Subshell Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this subshell command?
Consider the following bash script snippet. What will be printed to the terminal?
Bash Scripting
count=5 (echo $count; count=10; echo $count) echo $count
Attempts:
2 left
💡 Hint
Remember that commands inside parentheses run in a subshell, so variable changes inside do not affect the parent shell.
✗ Incorrect
The variable 'count' is initially 5. Inside the subshell, it prints 5, then changes to 10 and prints 10. Outside, the original 'count' remains 5, so the last echo prints 5.
💻 Command Output
intermediate1:30remaining
How many lines will this command output?
What is the number of lines printed by this command?
(echo "line1"; echo "line2"; echo "line3") | wc -l
Bash Scripting
(echo "line1"; echo "line2"; echo "line3") | wc -l
Attempts:
2 left
💡 Hint
The subshell groups commands and pipes their combined output to wc -l.
✗ Incorrect
The subshell runs three echo commands, each printing a line. The pipe sends all three lines to wc -l, which counts lines and outputs 3.
🔧 Debug
advanced2:30remaining
Why does this script print '0' instead of '3'?
This script intends to count files but prints 0. Why?
count=0
(ls *.txt; count=$(ls *.txt | wc -l))
echo $count
Bash Scripting
count=0
(ls *.txt; count=$(ls *.txt | wc -l))
echo $countAttempts:
2 left
💡 Hint
Think about where variable assignments inside parentheses take effect.
✗ Incorrect
The parentheses create a subshell. Variable assignments inside it do not change the parent shell's variables. So 'count' outside remains 0.
🚀 Application
advanced2:00remaining
Which command correctly groups commands to redirect combined output?
You want to run two commands and redirect both their outputs to a file. Which option does this correctly?
Attempts:
2 left
💡 Hint
Grouping commands with parentheses runs them in a subshell and redirects all output together.
✗ Incorrect
Option B runs both echo commands in a subshell and redirects all output to the file once. Option B uses braces which also groups commands but requires spaces and a semicolon; however, the question asks for subshell grouping.
🧠 Conceptual
expert3:00remaining
What is the effect of this command on the variable 'var'?
Given this script:
var=1
(var=5; echo $var)
echo $var
What will be printed and why?
Bash Scripting
var=1 (var=5; echo $var) echo $var
Attempts:
2 left
💡 Hint
Variables changed inside parentheses do not change outside variables.
✗ Incorrect
The subshell changes 'var' to 5 and prints it. Outside, 'var' remains 1, so the last echo prints 1.