sysxplore

sysxplore

Share this post

sysxplore
sysxplore
Bash 101: What are Subshells
Copy link
Facebook
Email
Notes
More

Bash 101: What are Subshells

TRÄW🤟's avatar
TRÄW🤟
Jun 09, 2025
∙ Paid
4

Share this post

sysxplore
sysxplore
Bash 101: What are Subshells
Copy link
Facebook
Email
Notes
More
1
Share

Subshells are a fundamental concept in Bash scripting that can be both powerful and confusing, especially for beginners. They allow you to create isolated execution environments within your scripts, providing a way to effectively manage processes and execute commands without affecting the parent shell's environment. This guide aims to provide a comprehensive understanding of subshells in Bash, including their creation, behavior, and practical applications.

What Exactly is a Subshell?

A subshell, also known as a child shell, is a separate instance of the shell that is spawned from the current shell process. It inherits the environment and variables from its parent shell but operates independently, allowing for isolated execution of commands and scripts. When a subshell is created, it runs in a separate process, distinct from the parent shell. This means that any changes made to the environment within the subshell, such as modifying variables or defining functions, are isolated and do not persist in the parent shell after the subshell terminates.

Creating a Subshell

There are several ways to create a subshell in Bash, each with its own nuances and use cases:

Parentheses or Curly Braces

The commands enclosed within parentheses are executed in a subshell. This is one of the most common and straightforward ways to create a subshell in Bash.

# Create a subshell
$ (pwd; ls; whoami)

Using curly braces {...} around a set of commands can also create a subshell:

$ { sleep 3; echo "Hello from subshell"; } 
# Subshell created

$ echo "Back in parent shell"

Command Substitution

# Assign the output of a subshell to a variable
$output=$(pwd; ls; whoami)

Command substitution creates a subshell and captures its output, which can be assigned to a variable or used in another command.

Explicit Subshell Invocation

The bash built-in command can be used to start a subshell and execute commands within it explicitly. The -c option allows you to specify the commands to be executed.

# Execute a subshell
$ bash -c "ls; whoami"

Relationship Between Parent Shell and Subshell

The parent shell and its subshells have a hierarchical relationship. As I have mentioned, the subshell inherits the environment variables, functions, and other settings from the parent shell, but any modifications made to the environment within the subshell are isolated and do not affect the parent shell.

To demonstrate this isolation, consider the following example:

# Parent shell
$ echo "Parent Shell: Value of  is $pshell"
  

# Create a subshell and modify pshell
$ (pshell=10; echo "Subshell: Value of pshell is $pshell")  

# Check the value of pshell in the parent shell
$ echo "Parent Shell: Value of pshell is $pshell"  

In this example, the variable pshell is first unset in the parent shell, meaning it has no value. Within the subshell created by the parentheses(), we assigned the value 10 to the variable pshell , and print its value. However, after the subshell terminates, the parent shell's value ofpshell remains unset, demonstrating the isolation of the subshell's environment.

It's important to note that subshells isolate the environment and affect the scope of variables and functions defined within them. Variables and functions defined in a subshell are not accessible outside of that subshell, even if the subshell is part of a larger script or function.

single subshell

How to Check if a Subshell Has Been Spawned

You can check if the current shell is a subshell by inspecting the value of the $BASH_SUBSHELL environment variable. This variable is set to a non-zero value in subshells, indicating the nesting level.

Keep reading with a 7-day free trial

Subscribe to sysxplore to keep reading this post and get 7 days of free access to the full post archives.

Already a paid subscriber? Sign in
© 2025 TRÄW🤟
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture

Share

Copy link
Facebook
Email
Notes
More