Scope of script variables and function variables in Shell

Source: Internet
Author: User

Defining functions in shell can modularize the code and facilitate code reuse. However, the scope of the variables in the script itself and the variables in the function may be confusing to you. Here, let's take a look at this issue.

(1) The variables defined in the shell script are global, and their scopes start from the defined place to the end of the shell or the deleted place.

Example 1: Scope of the script variable
#! /Bin/bash
# Define the function ltx_func
Ltx_func ()
{
Echo $ V1
# Modify the variable V1
V1. = 200
}
# Define the variable V1
V1. = 100
# Call the function ltx_func
Ltx_func
Echo $ V1

Result:
100
200
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 variables defined by the function
#! /Bin/bash
# Define the function ltx_func
Ltx_func ()
{
# Define the variable v2
V2 = 200
}
# Call the function ltx_func
Ltx_func
Echo $ v2

Result:
200
Resolution: function variable V2 is global by default, and its scope starts from "place where variable definition is executed when the function is called" and ends with 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 variables
#! /Bin/bash
# Define the function ltx_func
Ltx_func ()
{
# Define the local variable v2
Local v2 = 200
}
# Call the function ltx_func
Ltx_func
Echo $ v2

Result:
(Null)
Resolution: function variable V2 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_func
Ltx_func ()
{
Echo "Param 1: $1"
}
# Call the function ltx_func
Ltx_func 100

Result:
100
Resolution: The function parameters are local and accessed 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: shielding global variables with local variables of the same name
#! /Bin/bash
# Define the function ltx_func
Ltx_func ()
{
Echo $ V1
# Define the local variable V1
Local V1 = 200
Echo $ V1
}
# Define the global variable V1
V1. = 200
# Call the function ltx_func
Ltx_func
Echo $ V1

Result:
100
200
100
Resolution: the scope of global 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 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.

Defining functions in shell can modularize the code and facilitate code reuse. However, the scope of the variables in the script itself and the variables in the function may be confusing to you. Here, let's take a look at this issue.

(1) The variables defined in the shell script are global, and their scopes start from the defined place to the end of the shell or the deleted place.

Example 1: Scope of the script variable
#! /Bin/bash
# Define the function ltx_func
Ltx_func ()
{
Echo $ V1
# Modify the variable V1
V1. = 200
}
# Define the variable V1
V1. = 100
# Call the function ltx_func
Ltx_func
Echo $ V1

Result:
100
200
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 variables defined by the function
#! /Bin/bash
# Define the function ltx_func
Ltx_func ()
{
# Define the variable v2
V2 = 200
}
# Call the function ltx_func
Ltx_func
Echo $ v2

Result:
200
Resolution: function variable V2 is global by default, and its scope starts from "place where variable definition is executed when the function is called" and ends with 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 variables
#! /Bin/bash
# Define the function ltx_func
Ltx_func ()
{
# Define the local variable v2
Local v2 = 200
}
# Call the function ltx_func
Ltx_func
Echo $ v2

Result:
(Null)
Resolution: function variable V2 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_func
Ltx_func ()
{
Echo "Param 1: $1"
}
# Call the function ltx_func
Ltx_func 100

Result:
100
Resolution: The function parameters are local and accessed 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: shielding global variables with local variables of the same name
#! /Bin/bash
# Define the function ltx_func
Ltx_func ()
{
Echo $ V1
# Define the local variable V1
Local V1 = 200
Echo $ V1
}
# Define the global variable V1
V1. = 200
# Call the function ltx_func
Ltx_func
Echo $ V1

Result:
100
200
100
Resolution: the scope of global 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 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.