0
0
Bash Scriptingscripting~20 mins

Subshells (command grouping) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Subshell Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2: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
A
5
5
5
B
5
10
10
C
5
10
5
D
10
10
10
Attempts:
2 left
💡 Hint
Remember that commands inside parentheses run in a subshell, so variable changes inside do not affect the parent shell.
💻 Command Output
intermediate
1: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
AError
B1
C0
D3
Attempts:
2 left
💡 Hint
The subshell groups commands and pipes their combined output to wc -l.
🔧 Debug
advanced
2: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 $count
ABecause 'count' is set inside a subshell, it does not affect the parent shell variable.
BBecause 'ls *.txt' returns no files, so count is zero.
CBecause the syntax for command substitution is incorrect.
DBecause 'echo $count' is inside the subshell and prints zero.
Attempts:
2 left
💡 Hint
Think about where variable assignments inside parentheses take effect.
🚀 Application
advanced
2: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?
Aecho "Hello"; echo "World" > output.txt
B(echo "Hello"; echo "World") > output.txt
Cecho "Hello" > output.txt; echo "World" > output.txt
D{ echo "Hello"; echo "World"; } > output.txt
Attempts:
2 left
💡 Hint
Grouping commands with parentheses runs them in a subshell and redirects all output together.
🧠 Conceptual
expert
3: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
A
5
1
Because the assignment inside the subshell does not affect the parent shell variable.
B
1
1
Because the subshell does not print anything.
C
1
5
Because the first echo is outside the subshell.
D
5
5
Because the variable is changed globally.
Attempts:
2 left
💡 Hint
Variables changed inside parentheses do not change outside variables.