What is Subshell in Linux?

Linux TLDR
Last Updated:
Reading time: 3 minutes

When you interactively login to your system, you usually interact with your main shell from the terminal to execute commands; however, from your main shell (or parent shell), you can spawn a subshell (or child shell).

Confused? I knew this would be more difficult, especially if you are a beginner, but let’s start with the basics.

What is a Parent Shell?

Imagine, you booted your system and used the β€œCtrl+Alt+t” keyboard shortcut to launch your terminal emulator (like Gnome terminal or Konsole), and now you are executing a series of commands.

However, keep in mind that your terminal uses a shell such as Sh, Bash, or Zsh (depending on your login shell) to carry your commands to the kernel and return the processed result on the terminal screen.

Now, the shell you’re using to run your command is called the β€œmain shell” (or β€œparent shell”). It loads the environment variables and shell profiles needed for interactive login.

What is a Subshell?

From your β€œparent shell”, you can initiate a new shell (referring to the β€œsubshell”) to run your desired program, but note that the β€œsubshell” only accesses the global variables set by the β€œparent shell”, not the local variables.

Apart from that, changes made to global variables inside the β€œsubshell” are not reflected in the parent shell, but nested shells created inside the β€œsubshell” will inherit the changes.

How to Initiate a Subshell

The easiest and most traditional way is to execute your shell command; for example, if your default shell is bash, then execute the bash command in your parent shell to initiate a subshell.

πŸ“
Opening a new tab from the terminal will not create a subshell.

Also note that whenever you run a shell script, it runs its own shell (basically a subshell) depending on the shebang line (if not derived, then your default shell will be used) inside the shell script.

Since this shell is derived from the parent shell, the script is able to access all the parent shell environment variables except for local variables.

Accessing Environment Variables from the Subshell

The following is the basic example, where I’ve created a local β€œsite” variable and am trying to access it inside the subshell using β€œscript.shβ€œ.

Accessing local variables in the subshell

As you noted, the β€œsite” variable was accessible in the parent shell, but when you execute the shell script, it initiates its own shell (or subshell), from which it cannot access the local variable initiated from the parent shell.

To make it accessible within the subshell, you can use the export command to create a global variable that is accessible inside the subshell.

πŸ’‘
If you run your script like this: β€œ. script.shβ€œ, instead of creating a subshell, it will run in the parent shell and inherit even the local variables.
Accessing global variable in subshell

I believe that the examples will assist you in understanding how the script behaves when run in the subshell.

Next, let’s see the workings of nested subshells.

Nested Subshell

You already know that the subshell can use the global variables of the parent shell. However, if you change the value of a global variable in the subshell, the change won’t show up in the parent shell; it will only show up while the subshell session is running.

Accessing the modified variable value in the subshell from the parent shell

As you can see from the above picture, that β€œsite” modified value was only accessible while the subshell session was running; once you quit the session using the exit command, the value will change back to the original.

However, the β€œsite” modified value will be accessible inside the nested subshell (originated from the subshell), but if you modify the value of β€œsite” inside the nested subshell, then the changes will remain until the nested session is running; once you quit, it will change back to the previous value.

Accessing the modified variable value in a nested shell from a subshell

As you can see from the above picture, the modified value is only accessible in the current shell or the shell that originated from the current shell; the previous shell (or parent shell) does not inherit the changes made in the child shell (or subshell).

Final Tips!

The subshell is interesting to know as a beginner but rarely comes into use unless you are working with environment variables, aliases, or shell script.

If you have any questions related to this topic, feel free to ask them in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.