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.
Note: 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“.
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.
Note: 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.
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.
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.
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.
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).
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.