Chapter 5. Redirection, Pipe and Shell Script

Shell Variable and Environmental Variable

Shell Variable and Environmental Variable
Tag:

There are two types of variables in Linux – shell variables and environmental variables. Shell variables are only defined for the current shell and the variables cannot be used in other shells or processes. On the other hand, environmental variables are variables that are also defined for other shells or processes, and not just the current one. The environmental variables are inherited by any child shells or processes.

Declaration of variables

The ways to declare shell variables and environmental variables are different.

Shell variable: connect a variable and an assigned string with =. For example, abc=2023.

Shell-Variable-and-Environmental-Variable

Environmental variable: use the export command and connect a variable and an assigned string with =. For example, export ABC=1988.

Shell-Variable-and-Environmental-Variable

Inheritance of variables

As explained above, the key difference occurs if the variables are inherited in child processes. To see the difference, create two shell scripts.

First, create a shell script for the parent process.

Command Line - INPUT
vim variable_parent.sh

In the variable_parent.sh file, declare two types of variables.

  • abc : a shell variable
  • ABC : an environmental variable

In the parent shell script below, we’ll add a command to run a child script (variable_child.sh).

variable_parent.sh
#!/bin/bash
  
#shell variable
abc=2023

#environmental variable
export ABC=1988

echo "A shell variable in the parent shell script"
echo abc=$abc
echo "An environmental variable in the parent shell script"
echo ABC=$ABC

#run child script
./variable_child.sh

Next, create a shell script for the child process.

Command Line - INPUT
vim variable_child.sh

In the variable_child.sh file, add the echo commands to call the two variables declared in the parent script.

variable_child.sh
#!/bin/bash
  
echo "A shell variable in the child script"
echo abc=$abc
echo "An environmental variable in the child script"
echo ABC=$ABC

Add execution permissions to these files and execute the parent shell script.

Command Line - INPUT
sudo chmod u+x variable_parent.sh
sudo chmod u+x variable_child.sh
./variable_parent.sh

You can see that the values (2023 and 1988) assigned to both variables (abc and ABC) in the parent shell script are properly returned; however, the value assigned to abc (shell variable) in the child script is blank, while the value assigned to ABC (environmental variable) is properly returned.

Command Line - RESPONSE
A shell variable in the parent shell script
abc=2023
An environmental variable in the parent shell script
ABC=1988
A shell variable in the child script
abc=
An environmental variable in the child script
ABC=1988

From this example, you can see that shell variables are only working within the same process, while environmental variables are inherited in child processes.

There are two types of variables in Linux – shell variables and environmental variables. Shell variables are only defined for the current shell and the variables cannot be used in other shells or processes. On the other hand, environmental variables are variables that are also defined for other shells or processes, and not just the current one. The environmental variables are inherited by any child shells or processes.

Declaration of variables

The ways to declare shell variables and environmental variables are different.

Shell variable: connect a variable and an assigned string with =. For example, abc=2023.

Shell-Variable-and-Environmental-Variable

Environmental variable: use the export command and connect a variable and an assigned string with =. For example, export ABC=1988.

Shell-Variable-and-Environmental-Variable

Inheritance of variables

As explained above, the key difference occurs if the variables are inherited in child processes. To see the difference, create two shell scripts.

First, create a shell script for the parent process.

Command Line - INPUT
vim variable_parent.sh

In the variable_parent.sh file, declare two types of variables.

  • abc : a shell variable
  • ABC : an environmental variable

In the parent shell script below, we’ll add a command to run a child script (variable_child.sh).

variable_parent.sh
#!/bin/bash
  
#shell variable
abc=2023

#environmental variable
export ABC=1988

echo "A shell variable in the parent shell script"
echo abc=$abc
echo "An environmental variable in the parent shell script"
echo ABC=$ABC

#run child script
./variable_child.sh

Next, create a shell script for the child process.

Command Line - INPUT
vim variable_child.sh

In the variable_child.sh file, add the echo commands to call the two variables declared in the parent script.

variable_child.sh
#!/bin/bash
  
echo "A shell variable in the child script"
echo abc=$abc
echo "An environmental variable in the child script"
echo ABC=$ABC

Add execution permissions to these files and execute the parent shell script.

Command Line - INPUT
sudo chmod u+x variable_parent.sh
sudo chmod u+x variable_child.sh
./variable_parent.sh

You can see that the values (2023 and 1988) assigned to both variables (abc and ABC) in the parent shell script are properly returned; however, the value assigned to abc (shell variable) in the child script is blank, while the value assigned to ABC (environmental variable) is properly returned.

Command Line - RESPONSE
A shell variable in the parent shell script
abc=2023
An environmental variable in the parent shell script
ABC=1988
A shell variable in the child script
abc=
An environmental variable in the child script
ABC=1988

From this example, you can see that shell variables are only working within the same process, while environmental variables are inherited in child processes.

Tag: