Detailed explanation and examples of function variable scope, variable type, common variable, global variable, environment variable and local variable in shell

Overview of function variables

Variable scope: the scope of Shell variables is the effective scope of Shell variables

Variable type

  • Common variables are also called global variables. The scope of common variables is the current shell script program file, including the functions in the script.

  • Environment variables: the current shell and sub shell are valid, and variables can be used in sub processes.

  • Local variable: the variable can only be used inside the function, accompanying the life cycle of the function; The variable is automatically destroyed at the end of the function

Note: if a common variable is defined in the function and the name is the same as the local variable, the local variable is used.

Common variable (global variable)

Ordinary variables are valid for the current shell. The variables defined in the shell are global variables by default. Each process has its own scope and does not affect each other.

  • Two terminals
    Open two terminal windows, define a variable a, and then print out. Another terminal window print is empty.
Terminal 1️⃣
[root@mdns zaishu]#a=10
[root@mdns zaishu]#echo $a
10
Terminal 2️⃣
[root@mdns zaishu]#echo $a

[root@mdns zaishu]#

Note that the global variable is only valid in the first Shell process that defines it, and has no impact on the new Shell process. The scope of the global variable is the current Shell process. When a Shell window is opened, a Shell process is created, and when multiple Shell windows are opened, multiple Shell processes are created. Each Shell process is independent and has a different process ID.

  • Terminal and executed script
    Executing the script in the shell is equivalent to opening a new sub shell, which is a different process from the current shell. Therefore, ordinary variables will not act in the script.
#!/bin/bash
echo "$a"
b=20
[root@mdns zaishu]#a=10	#Define common variables
[root@mdns zaishu]#./funs	#Executing the script found that the value of a is empty and does not affect each other. Executing the script is equivalent to starting the shell process alone 

[root@mdns zaishu]#echo $b	#The output is empty
  • Change from ordinary variable to environment variable
    Use the source command to execute multiple Shell script files in the current Shell process. At this time, the global variables are valid in these script files.

For example, there are now two Shell script files, a.sh and b.sh.

fun1.sh The codes are as follows:
#!/bin/bash
echo $a
b=20
fun2.sh The codes are as follows:
#!/bin/bash
echo $b

Open a Shell window and enter the following command:

[root@mdns zaishu]#a=99
[root@mdns zaishu]#source fun1.sh
99

[root@mdns zaishu]#source fun2.sh
20
[root@mdns zaishu]#echo $b
20

Three commands are executed in a process. From the output results, it can be found that these variables are equivalent to environment variables, which are effective for the current process and child processes.

environment variable

Ordinary variables are only valid in the current Shell process, and are not valid for other Shell processes and child processes. The pass environment variable is also valid for all child processes.

The Shell process in which the environment variable is created is called the parent process. If a new process is created in the parent process to execute the Shell command, the new process is called the Shell child process. When a Shell child process is generated, it will inherit the environment variables of the parent process for its own use, so the environment variables can be passed from the parent process to the child process.

The easiest way to create a Shell subprocess is to run the bash command. You can exit the Shell layer by layer through the exit command.

  • Demonstration of environment variables:
[root@mdns zaishu]#a=10  #Define a global variable
[root@mdns zaishu]#echo $a
10
[root@mdns zaishu]#bash #Enter child process
[root@mdns zaishu]#echo $a #No output
[root@mdns zaishu]#

By default, a is invalid in Shell subprocesses; After using export to export a as an environment variable, it can be used in the child process.

[root@mdns zaishu]#a=10
[root@mdns zaishu]#export a #Export a as an environment variable
[root@mdns zaishu]#bash
[root@mdns zaishu]#echo $a
10

export a can export it as an environment variable. If it is also defined as an environment variable, it can be written as
export a=10

  • Permanent environment variable
    The environment variables exported by export are only valid for the current Shell and all child processes. If the parent process at the top level is closed, the environment variables will disappear and other processes will not be able to use them. Therefore, the environment variables are also temporary.
    Writing variables to the Shell configuration file can permanently take effect as environment variables.
[root@mdns zaishu]#cat ~/.bash_profile 
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin:/usr/local/mysql/bin

export PATH
export a=10
[root@mdns zaishu]#source ~/.bash_profile 
[root@mdns zaishu]#echo $a
10

local variable

Variables defined in the Shell function are global variables by default, which has the same effect as variables defined outside the function.

#!/bin/bash
#Define function
function func() {
    a=18
}
#Call function
func
#Variables inside the output function
echo $a
[root@mdns zaishu]#./funs2.sh 
18

a is defined inside the function, but its value can also be obtained outside the function to prove that its scope is global, not limited to the inside of the function.

To limit the scope of a variable to the inside of a function, you can add the local command when defining it. At this time, the variable becomes a local variable.

#!/bin/bash
#Define function
function func() {
    local a=18 #Defined as a local variable
}
#Call function
func
#Variables inside the output function
echo $a

The output result is empty, indicating that variable a is invalid outside the function and is a local variable.

[root@mdns zaishu]#./funs2.sh 
[root@mdns zaishu]#

Keywords: Linux Operation & Maintenance shell bash

Added by Illusionist on Wed, 02 Feb 2022 12:15:01 +0200