Local variables locally in shell scripts

Source: Internet
Author: User

Local variables in shell scripts define functions in the shell to make the code modular and easy to reuse 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 can not 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.

The shell supports custom variables. Variable names do not add dollar signs ($) When defining variable definition variables, such as: VariableName="value"Note that there can be no spaces between the variable name and the equals sign, which may be different from any programming language you are familiar with. At the same time, the name of the variable names should follow the following rules: The first character must be a letter (a-z,a-Z).    You can use an underscore (_) without spaces in the middle.    Punctuation cannot be used. You can't use the keywords in bash (you can see the reserved keywords using the help command). Example of variable definition: Myurl="http://see.xidian.edu.cn/cpp/linux/"MyNum= -Use variables with a defined variable, as long as the variable name is preceded by a dollar sign ($), such as: Your_Name="Mozhiyan"echo $your _name echo ${your_name} The curly braces outside of the variable name are optional and add no lines, and curly braces are used to help the interpreter identify the bounds of the variable, such as the following: forSkillinchAda coffe Action Java DoEcho"I am good at ${skill}script"Done If you do not add curly braces to the skill variable, write echo"I am good at $skillScript", the interpreter will treat $skillscript as a variable (whose value is null), and the result of the code execution is not what we expect it to look like. It is a good programming habit to add curly braces to all variables. Redefine variables that have been defined and can be redefined, such as: Myurl="http://see.xidian.edu.cn/cpp/linux/"Echo ${myurl} myurl="http://see.xidian.edu.cn/cpp/shell/"Echo ${myurl} It is legal to write this, but note that the second assignment cannot be written $myUrl="http://see.xidian.edu.cn/cpp/shell/", the dollar symbol ($) is used when using variables. Read-only variable usageReadOnlyA command can define a variable as a read-only variable, and the value of a read-only variable cannot be changed. The following example attempts to change a read-only variable, resulting in an error: #!/bin/Bash Myurl="http://see.xidian.edu.cn/cpp/shell/"    ReadOnlyMyurl Myurl="http://see.xidian.edu.cn/cpp/danpianji/"run the script with the following results:/bin/sh:name:this variable isRead only. Delete variables use the unset command to delete variables. Syntax: unset variable_name variable cannot be used again after it is deleted; unset command cannot delete read-only variable. As an example: #!/bin/SH myurl="http://see.xidian.edu.cn/cpp/u/xitong/"unset Myurl Echo $myUrl The script above does not have any output. Variable type when you run the shell, there are three variables at the same time:1Local variable local variables are defined in a script or command, only valid in the current shell instance, and other shell-initiated programs cannot access local variables. 2environment variables All programs, including shell-initiated programs, can access environment variables, and some programs require environment variables to keep them running properly. Shell scripts can also define environment variables when necessary. 3Shell variable shell variables are special variables that are set by the shell program. Some of the shell variables are environment variables, some of which are local variables that guarantee the shell's normal operation.

Local variables locally 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.