In this article, we cover how to start a Subshell in Linux. A subshell is an instance of the parent shell.
How to start a Subshell in Linux
We can start a Subshell through the bash command itself. Open a terminal and issue the following:
bash
And, to exit from the subshell, issue:
exit
It won’t start a subshell in a new window. So, if we start multiple of those. Then, it would get tough to identify the Subshell level.
To identify the Subshell level, we have the variable:
SHLVL
So, to identify the Subshell level:
echo $SHLVL
1 value here shows that we are in the parent shell. Now, start a new shell with the command:
bash
This time around the value 2 will be there in the standard output if we issue the command:
echo $SHLVL
It shows that we are in a Subshell. Try issuing the bash command again and this time SHLVL would give us 3. For every Subshell started the SHLVL value increments by 1.
Apart from that, we can also access the parent shell variable in a subshell. We have covered that in the article: Access parent shell variable in a subshell.
In conclusion, we have covered here how to start a Subshell in Linux here.
Additional Info:
Not related to Subshells. But, quite a useful feature. To change the directory, we use the cd command-line utility. At times, we are required to move to the parent directory of our current working directory. For instance, if we wish to move from /home/$USER/Downloads/new-dir to /home/$USER/Downloads/
Then, we can use the cd .. command:
cd ..
Make sure that there is a space between the Double dot(..) and the cd command.
If you wish to move up further, then again use the:
cd ..
We can use the following command to print the current working directory in the standard output:
pwd