📖 Scenario: You are organizing a simple bash script to manage files in a folder. You want to group commands together so they run in a subshell, keeping your main shell environment clean.
🎯 Goal: Build a bash script that creates a directory, changes into it, creates a file, and lists the contents, all inside a subshell.
📋 What You'll Learn
Create a variable called folder with the value test_dir
Create a subshell that runs these commands: mkdir $folder, cd $folder, touch file.txt, and ls
Print the current directory outside the subshell to show it did not change
💡 Why This Matters
🌍 Real World
Grouping commands in subshells helps keep your main shell environment clean and avoids side effects like changing directories permanently.
💼 Career
Many automation scripts use subshells to run grouped commands safely without affecting the main script environment, which is important for reliable system administration and deployment tasks.
Progress0 / 4 steps
1
Create the folder variable
Create a variable called folder and set it to the string test_dir.
Bash Scripting
Hint
Use folder="test_dir" to create the variable.
2
Write a subshell to create and enter the folder
Write a subshell using parentheses ( ) that runs these commands in order: mkdir $folder, cd $folder, touch file.txt, and ls.
Bash Scripting
Hint
Use parentheses to group commands: (mkdir "$folder" && cd "$folder" && touch file.txt && ls).
3
Print the current directory outside the subshell
Add a command to print the current directory using pwd outside the subshell to show it did not change.
Bash Scripting
Hint
Simply add pwd after the subshell to print the current directory.
4
Run the script and observe output
Run the script and observe the output. The subshell should list file.txt, and the pwd command should show your original directory.
Bash Scripting
Hint
The output should show file.txt from the subshell ls command, then your current directory path from pwd.
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.