music |
| | OSdata.com |
shell levels and subshells
summary
This subchapter looks at shell levels and subshells.
shell levels and subshells
This subchapter looks at shell levels and subshells.
As you advance to more sophisticated operations, such as the material on substitutions and variables, you will find that the bash shell has levels.
Every program, command, or script that you run can have and modify variables. How does Unix/Linux keep programs, commands, and scripts from interfering with each others variables?
The answer is shell levels. Each time the shell starts a program, command, or script, it starts it in a subshell. The subshell has its own variables and environment. It cant interfere with any other program, command, or script, even if they use the same name for a variable.
Most of the time this system is great It can lead to frustration when attempting to share variables (you will have to use some work-around). And it can trip up beginners who arent expecting this.
Technically, each subshell is run as a child process. We will discuss this more in the subchapter on processes.
shell levels
You can use the $SHLVL built-in variable to detemrine the current shell level.
In the following example, we will start up bash in a subshell. Yes, you can start-up another copy of bash from the current copy of bash.
This example includes some materials that will be covered in more detail in later subchapters.
$ echo $SHLVL
1
$ bash
$ echo $SHLVL
2
$ exit
$ echo $SHLVL
1
$
The first echo command shows the current shell level (1).
The bash command starts up another copy of bash in a subshell.
The second echo command shows the subshell level (2).
The exit command closes down the copy of bash running in the child process and returns us to the main copy of bash.
The third echo command shows the original shell level (1).
comments, suggestions, corrections, criticisms
free music player coding example
Coding example: I am making heavily documented and explained open source code for a method to play music for free almost any song, no subscription fees, no download costs, no advertisements, all completely legal. This is done by building a front-end to YouTube (which checks the copyright permissions for you).
View music player in action: www.musicinpublic.com/.
Create your own copy from the original source code/ (presented for learning programming).
Because I no longer have the computer and software to make PDFs, the book is available as an HTML file, which you can convert into a PDF.
Names and logos of various OSs are trademarks of their respective owners.