In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to understand the scope of script variables and function variables. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it with the editor.
Defining functions in shell can modularize the code and facilitate code reuse. However, it may be difficult for you to understand the scope of the variables of the script itself and the variables of the function. Let's sort out the problem here.
(1) the variable defined in the Shell script is global, and its scope starts from the place where it is defined to the place where the shell ends or where it is shown to be deleted.
Example 1: scope of script variables
#! / bin/bash
# define the function ltx_func
Ltx_func ()
{
Echo $v1
# modify the variable v1
V1o200
}
# define the variable v1
V1mm 100
# call the function ltx_func
Ltx_func
Echo $v1
Results:
one hundred
two hundred
Parsing: the scope of the script variable v1 starts where it is defined and ends with shell. The place where the function ltx_func is called is within the scope of the variable v1, so you can access and modify the variable v1.
(2) the variable defined by the Shell function is global by default, and its scope starts from "where the variable definition is executed when the function is called" to the end of the shell or where it is shown to be deleted. Variables defined by a function can be displayed and defined as local, and their scope is limited to the function. Note, however, that the argument to the function is local.
Example 2: global variable defined by a function
#! / bin/bash
# define the function ltx_func
Ltx_func ()
{
# define the variable v2
V2o200
}
# call the function ltx_func
Ltx_func
Echo $v2
Results:
two hundred
Parsing: the 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 that it doesn't start where the function is defined, but where the function is called. The print command is in the scope of the variable v2, so you can access the variable v2.
Example 3: local variable defined by a function
#! / bin/bash
# define the function ltx_func
Ltx_func ()
{
# define the local variable v2
Local v2o200
}
# call the function ltx_func
Ltx_func
Echo $v2
Results:
(empty)
Parsing: the function variable v2 is defined as local, and its scope is limited to the function. The print command is outside the function and is not in the scope of the variable v2, so the variable v2 can not be accessed.
Example 4: the function argument 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
Results:
one hundred
Parsing: the function argument is local and is accessed through the location variable. Print the first argument of the command output function.
(3) if it has the same name, the local variable defined by the Shell function will block the global variable defined by the script.
Example 5: local variable of the same name masks global variable
#! / bin/bash
# define the function ltx_func
Ltx_func ()
{
Echo $v1
# define the local variable v1
Local v1o200
Echo $v1
}
# define the global variable v1
V1o200
# call the function ltx_func
Ltx_func
Echo $v1
Results:
one hundred
two hundred
one hundred
Parsing: the scope of the global variable v1 starts where it is defined and ends with shell. The place where the function ltx_func is called is within the scope of the variable v1, so you can variable v1. The function also defines the local variable v1 with the same name, and the local variable with the same name shields the global variable, so the function accesses the local variable for the second time. Print v1 again after exiting the function, where the local variable defined by the function has disappeared and the global variable is accessed.
The above is how to understand the scope of script variables and function variables. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.