The scope of the script variables and function variables in Shell defines the function in shell to modularize the code and facilitate code reuse. However, the variables and functions of the script itself
The scope of the variable may be confusing. The variables defined in the www.2cto.com (1) Shell script are global, and the scope of the variables starts from the defined place to the end of the shell or
The deleted location is displayed. Example 1: Scope of the script variable #! /Bin/bash # define the function ltx_funcltx_func () {echo $ v1 # modify the variable v1 v1 = 200} # define the variable v1v1 = 100 # call the function ltx_funcltx_funcecho $ v1 result: 100200 parsing: The scope of the script variable v1 starts from the defined place and ends with the shell. The call function ltx_func is in the scope of the variable v1, so you can access and modify the variable v1. (2) The variables defined by Shell functions are global by default. The scope of the variables starts from the "place where the variable definition is executed when the function is called" to the end of shell or display the deletion location. The variables defined by the function can be displayed and defined as local, and their scope is limited to the function. Note that the function parameter is local. Example 2: global variable #! defined by the function #! /Bin/bash # define the function ltx_funcltx_func () {# define the variable v2 v2 = 200} # call the function ltx_funcltx_funcecho $ v2 result: 200 Resolution: function variable v2 is global by default, and its scope starts from "where the variable definition is executed when the function is called" to the end of shell. Note: It does not start from where the function is defined, but from where the function is called. The print command is in the scope of variable v2, so it can access variable v2. Example 3: function-defined local variable #! /Bin/bash # define the function ltx_funcltx_func () {# define the local variable v2 local v2 = 200} # call the function ltx_funcltx_funcecho $ v2 result :( empty) parsing: function variable v2 display is defined as local, and its scope is limited to the function. The print command is out of the function and is not in the scope of variable v2. Therefore, you cannot access variable v2. Example 4: The function parameter is a local variable #! /Bin/bash # define the function ltx_funcltx_func () {echo "param 1: $1"} # call the function ltx_funcltx_func 100 result: www.2cto.com 100 Resolution: the function parameter is local, access through location variables. Print the first parameter of the command output function. (3) If the name is the same, the local variable defined by the Shell function will block the global variable defined by the script. Example 5: The local variable with the same name shields the global variable #! /Bin/bash # define the function ltx_funcltx_func () {echo $ v1 # define the local variable v1 local v1 = 200 echo $ v1} # define the global variable v1v1 = 200 # call the function ltx_funcltx_funcecho $ v1 result: 100200100 Resolution: the scope of global variable v1 starts from the defined place and ends with shell. The call function ltx_func is in the scope of the variable v1, so the variable v1 can be called. The function defines the local variable v1 with the same name. The local variable with the same name shields the global variable. Therefore, the function prints the local variable for the second time. Print v1 again after exiting the function. The local variable defined by the function disappears and the global variable is accessed. Source http://blog.csdn.net/ltx19860420/article/details/5570902