A subshell is a separate child shell started by the main shell to run commands in isolation. It allows grouping commands so they run in a new shell environment without affecting the parent shell.
Click to reveal answer
beginner
How do you create a subshell to group commands in bash?
You create a subshell by enclosing commands in parentheses: ( command1; command2 ). This runs the commands inside a new shell process.
Click to reveal answer
intermediate
What happens to variable changes inside a subshell?
Changes to variables inside a subshell do not affect the parent shell. The subshell has its own copy of variables, so changes are lost when the subshell ends.
Click to reveal answer
intermediate
Why use subshells instead of just grouping commands with curly braces {}?
Subshells run commands in a separate environment, isolating changes like variable assignments or directory changes. Curly braces group commands but run in the current shell, so changes persist.
Click to reveal answer
beginner
Example: What is the output of this script?
x=5
( x=10; echo $x )
echo $x
Output:
10
5
Explanation: Inside the subshell, x is set to 10 and printed. Outside, x remains 5 because the subshell's change does not affect the parent shell.
Click to reveal answer
Which symbol is used to create a subshell in bash?
A{ }
B[ ]
C( )
D< >
✗ Incorrect
Parentheses ( ) create a subshell, running commands in a separate shell process.
If you change a variable inside a subshell, what happens to the variable outside?
AIt changes permanently
BIt causes an error
CIt becomes undefined
DIt remains unchanged
✗ Incorrect
Variables changed inside a subshell do not affect the parent shell's variables.
What is the main difference between using ( ) and { } for grouping commands?
A( ) runs commands in a subshell; { } runs in the current shell
B( ) runs commands faster than { }
C{ } creates a subshell; ( ) runs in current shell
DNo difference, both are the same
✗ Incorrect
Parentheses create a subshell; curly braces group commands but run in the current shell.
Which of these is a valid subshell command grouping?
A<cd /tmp; ls>
B(cd /tmp; ls)
C[cd /tmp; ls]
D{cd /tmp; ls}
✗ Incorrect
Only parentheses ( ) create a subshell to run commands in a new shell.
Why might you use a subshell when running commands?
ATo isolate changes so they don't affect the main shell
BTo speed up command execution
CTo permanently change environment variables
DTo avoid typing commands
✗ Incorrect
Subshells isolate changes like variable or directory changes from the main shell.
Explain what a subshell is and how it affects variable changes in bash scripting.
Think about running commands inside parentheses.
You got /3 concepts.
Describe the difference between grouping commands with ( ) and { } in bash.
Consider where changes like directory or variable assignments apply.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using a subshell in bash scripting with (commands)?
easy
A. To create a new user session
B. To run commands faster by parallel processing
C. To permanently change the current shell environment
D. To run grouped commands in a separate shell, isolating their effects
Solution
Step 1: Understand what a subshell does
A subshell runs commands inside parentheses in a new shell process separate from the current one.
Step 2: Identify the effect of running commands in a subshell
Changes like directory or variable updates inside a subshell do not affect the parent shell environment.
Final Answer:
To run grouped commands in a separate shell, isolating their effects -> Option D
The subshell inherits the variable var=hello. Inside the subshell, var is changed to world, but this change is local to the subshell.
Step 2: Trace the output commands
Inside subshell: echo $var prints 'hello' then 'world'. Outside subshell: echo $var prints original 'hello' because subshell changes do not affect parent shell.
Hint: Directory changes in subshell stay local [OK]
Common Mistakes:
Thinking cd inside subshell changes parent directory
Believing ls affects directory
Confusing variable scope with directory
5. You want to temporarily change directory and list files without affecting the current shell's directory. Which command grouping achieves this correctly?
hard
A. { cd /var/log && ls; }
B. (cd /var/log && ls)
C. cd /var/log; ls
D. cd /var/log && ls
Solution
Step 1: Identify command grouping that isolates directory change
Using parentheses runs commands in a subshell, so directory changes inside do not affect the parent shell.
Step 2: Analyze each option
(cd /var/log && ls) uses parentheses correctly. { cd /var/log && ls; } uses braces which run commands in current shell, changing directory permanently. Options C and D change directory in current shell without grouping.
Final Answer:
(cd /var/log && ls) -> Option B
Quick Check:
Parent directory unchanged with subshell ( ) [OK]
Hint: Use ( ) to isolate directory changes [OK]
Common Mistakes:
Using braces { } which affect current shell
Not grouping commands, so directory changes persist
Assuming && alone isolates commands
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.