Test Variable and Alias Inheritance in a Child Shell
In this step, you will explore how variables and aliases behave when you start a new shell from your current one. This new shell is called a "child shell," and the original shell is the "parent shell." This concept is crucial for understanding how shell environments are structured and how scripts execute. We will test whether the variables flower and nut from the previous step are passed down, or "inherited," by a child shell.
First, let's identify the Process ID (PID) of our current parent shell. Every process in Linux has a unique PID. You can view it with the echo $$ command.
echo $$
Your output will be a number, which is the PID of your current shell. For example:
123
Now, start a child shell by simply typing zsh and pressing Enter. This creates a new shell process inside your current one.
zsh
You are now in a new shell session. To confirm this, we can use the ps -f command, which shows detailed process information, including the Parent Process ID (PPID).
ps -f
Look at the output. You will see two zsh processes. The PID of the new zsh process should have a PPID that matches the PID of the parent shell you noted earlier.
UID PID PPID C STIME TTY TIME CMD
labex 123 1 0 10:00 pts/0 00:00:00 zsh
labex 456 123 0 10:01 pts/0 00:00:00 zsh
labex 457 456 0 10:01 pts/0 00:00:00 ps -f
In this example, the new shell (PID 456) is a child of the original shell (PPID 123).
Now, let's test the variables. In this child shell, try to display the value of the local variable flower.
echo $flower
The command produces no output. This is because local variables are confined to the shell in which they are created and are not inherited by child shells.
Next, check the environment variable nut.
echo $nut
This time, the shell prints the value of the variable:
almond
This demonstrates that environment variables, unlike local variables, are inherited by child shells.
Now, let's return to the parent shell by using the exit command.
exit
You are now back in your original shell. Let's perform a similar test with an alias. An alias is a shortcut for a command. Create an alias named ldetc that executes ls -ld /etc.
alias ldetc='ls -ld /etc'
Verify the alias was created by typing alias.
alias ldetc
You should see the definition of your alias:
ldetc='ls -ld /etc'
Now, test the alias by running it.
ldetc
The command will execute ls -ld /etc and show you the details of the /etc directory.
drwxr-xr-x 1 root root 4096 Oct 10 10:00 /etc
Now, open another child shell to see if the alias is inherited.
zsh
Inside the new child shell, try to use the ldetc alias.
ldetc
You will receive an error message, because aliases, like local variables, are not inherited by child shells.
zsh: command not found: ldetc
Return to the parent shell.
exit
Finally, let's clean up by removing the alias from the parent shell using the unalias command.
unalias ldetc
Verify its removal by trying to list it again.
alias ldetc
Nothing is returned.
This exercise has shown you a key principle of shell behavior: environment variables are inherited by child processes, but local variables and aliases are not.