Local variables in shell scripts

Source: Internet
Author: User

Defining functions in the shell allows the code to be modularized to facilitate reuse of code. But the scope of the variables and functions of the script itself can be confusing, and here's a look at the problem.

(1)The variables defined in the shell script are the global, whose scope starts at the defined place, until the end of the shell or where it is shown to be deleted.

Example 1: Scope of script variables
#!/bin/bash
#define The function Ltx_func
Ltx_func ()
{
echo $v 1
#modify the variable v1
v1=200
}
#define the variable v1
v1=100
#call the function Ltx_func
Ltx_func
echo $v 1

Results:
100
200
Parsing: The scope of the script variable v1 starts at the defined place and ends at the shell. Where the function ltx_func is called is within the scope of the variable v1, so the variable v1 can be accessed and modified.

(2)The variable defined by the Shell function is global by default, and its scope begins with the place where the function is called to execute the variable definition.To the end of the shell or to the point where the delete is displayed. The variables defined by the function can be displayed as local and scoped to the function. Note, however, that the parameters of the function are local.

Example 2: Global Variables for function definitions
#!/bin/bash
#define The function Ltx_func
Ltx_func ()
{
#define THE Variable v2
v2=200
}
#call the function Ltx_func
Ltx_func
Echo $v 2

Results:
200
Parsing: The function variable v2 is global by default and its scope starts at the place where the function is called to execute the variable definition, until the shell ends. Note that instead of starting from where the function is defined, it starts at the place where the function is called. The Print command is within the scope of the variable v2, so the variable v2 can be accessed.

Example 3: Local Variables for function definitions
#!/bin/bash
#define The function Ltx_func
Ltx_func ()
{
#define The local Variablev2
Local v2=200
}
#call the function Ltx_func
Ltx_func
Echo $v 2

Results:
Empty
Analytical:The function variable v2 display is defined as local, its scope is limited to the function inside。 The Print command is outside the function and is not within the scope of the variable v2, so the variable v2 cannot be accessed.

Example 4: The function parameter is a local variable
#!/bin/bash
#define The function Ltx_func
Ltx_func ()
{
echo "PARAM 1: $ $"
}
#call the function Ltx_func
Ltx_func 100

Results:
100
Analytical:The function parameter is local, accessed through positional variables. Prints the first parameter of a command output function.

(3)if the same name exists, the local variable defined by the Shell function masks the global variable defined by the script。

Example 5: local variable with the same name mask global variable
#!/bin/bash
#define The function Ltx_func
Ltx_func ()
{
echo $v 1
#define The local Variablev1
Local v1=200
echo $v 1
}
#define THE global variable v1
v1=100

#call the function Ltx_func
Ltx_func
echo $v 1

Results:
100
200
100
Parsing: The scope of the global variable V1 starts at the defined place and ends at the shell. The function ltx_func is called in the scope of the variable v1, so the variable can be v1. The function also defines the local variable v1 with the same name, and the local variable with the same name masks the global variable, so the function second print accesses the local variable. After exiting the function and printing the V1 again, the local variable defined by the function has disappeared and the global variable is accessed.

Local variables in shell scripts

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.