Skip to content
Learn Netverks

Lesson

Step 29/36 81% through track

subshells-bash

Subshells

Last reviewed May 28, 2026 Content v20260528
Track mode
none
Means
Read / quiz
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Subshells: the syntax, patterns, and safety habits you need before advancing in Bash.

Teams still ship Subshells in Bash codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Subshells in contexts like: CI jobs, server maintenance, local dev automation, and Git hooks.

Read each lesson, copy bash examples into your own terminal, and complete the lesson MCQs—there is no in-browser runner for security reasons.

When you can explain the previous lesson's ideas without copying starter code.

A subshell is a separate shell process created by ( ), pipelines, or command substitution. Variables set inside may not affect the parent.

Parentheses group

cd /tmp
( cd /var; pwd )
pwd

cd inside parentheses does not change the outer shell's directory.

Subshell vs current shell

count=0
( count=1 )
echo "outer count=$count"
{ count=1; }
echo "outer count=$count"

Braces { } run in the current shell if on the same line with semicolons—subtle syntax rules apply.

Background jobs

sleep 2 &
jobs
wait

& runs in background; wait blocks until jobs finish—useful for parallel steps with care.

Important interview questions and answers

  1. Q: Why ( cd /var ) leaves parent cwd?
    A: Subshell has its own working directory discarded on exit.
  2. Q: Background &?
    A: Runs command without blocking; returns job id.

Self-check

  1. What punctuation creates a subshell for cd?
  2. What command waits for background jobs?

Tip: Use subshells when you need temporary cd without affecting the parent shell.

Interview prep

( ) subshell?

Separate process; cd changes do not persist to parent.

& background?

Runs command without blocking the shell.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • ( ) vs { }?
  • cd in subshell?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump