Explain shell variables in detail


1. Introduction

Variable is an indispensable part of any programming language. Variable is used to store all kinds of data. Script language usually does not need to specify the type when defining variables. It can assign values directly. Shell variables also follow this rule.

In Bash shell, the value of every variable is a string. Whether you use quotation marks or not when you assign a value to a variable, the value will be stored as a string. This means that the bash shell does not differentiate between variable types by default, and even if you assign integers and decimals to variables, they are treated as strings, unlike most programming languages. Such as C / C + +, variables are divided into integer, floating point, string, Boolean and other types. Of course, if necessary, you can also use the declare / typeset shell built-in command to explicitly define the type of variable, but in general, there is no such requirement. Shell developers can pay attention to the type of value when writing code.

2. Variable type

According to the scope and life cycle of variables, shell variables can be divided into four categories

(1) Permanent environment variable: the configuration file needs to be modified, and the variable takes effect permanently.

(2) Temporary environment variable: it can be declared with the export command line. The variable is still valid after the shell script process ends, but it is invalid after closing the current shell session.

(3) Global variable: it is defined in the script and is only valid in the current shell script. Other shell script processes cannot access it. Its scope starts from the defined position to the end of the script or the place where it is deleted. Note that global variables can be defined either inside or outside the shell function, because the variables defined inside the shell function are global by default, and the scope starts from “where the variable definition is executed when the function is called” to the end of the script or where it is deleted.


Globalvar = dablelv # global variable

(4) Local variables. In the shell script, the variables defined with the local keyword are displayed in the function. Its scope is limited to the function. Local variables with the same name mask global variables.


function test()
	Local localvar = dablelv # local variable
Echo $localvar # output is empty

3. Define variables

Shell supports the following three ways to define variables:


VaR is the variable name and value is the value assigned to the variable. If value does not contain any white space (such as space, tab, etc.), quotation marks can be omitted; if value contains white space, quotation marks must be used. There is also a difference between using single quotation marks and using double quotation marks, which we will explain later. Note that there should be no spaces around the assignment number, which may be different from most programming languages you are familiar with.

The naming rules of shell variables are the same as most programming languages
(1) The variable name consists of numbers, letters and underscores;
(2) It must start with a letter or an underline;
(3) You can’t use the keywords in the shell (you can view the reserved keywords through the help command).

Example of variable definition:

Home = "Anhui"

4. Access variables

To use a defined variable, just add the dollar sign $in front of the variable name, such as:

echo $name
echo ${name}

The curly bracket {} outside the variable name is optional, whether it is added or not. Curly brackets are added to help the interpreter identify the boundary of the variable, such as the following case:

echo "I am good at ${skill}Script"

If you don’t bracket the skill variable, the interpreter will$skillScriptAs a variable (its value is empty), the result of code execution is not what we expect.

It’s recommended to use curly braces {} for all variables, which is a good programming habit.

5. Modify the value of the variable

The defined variables can be re assigned, such as:

echo $name

When assigning a value to a variable for the second time, it cannot be added before the variable name. It can only be added when the variable is used. It can only be added when the variable is used. It can only be added when the variable is used.

6. The difference between single quotation mark and double quotation mark

When defining a variable, the value of the variable can be surrounded by single quotation marks or double quotation marks. What’s the difference between them? Take the following code as an example:


STR1 ='My name is ${name} '
STR2 = "my name is ${name}"
echo $str1
echo $str2

Results of operation:

My name is ${name}
My name is dablelv

When you surround the value of a variable with single quotation marks, you can output whatever is in the single quotation marks. Even if there are variables and commands in the content (the command needs to be inverted), they will be output as they are. This method is more suitable for defining the situation of displaying pure string, that is, the scene where you do not want to parse variables, commands, etc.

When the value of a variable is surrounded by double quotation marks, the variables and commands in the variable will be parsed first, instead of outputting the variable names and commands in double quotation marks as they are. This method is more suitable for variable definitions with variables and commands attached to a string that you want to parse and then output.

Suggestions: if the content of a variable is a number, you can use no quotation marks; if you really need to output as is, you can use single quotation marks; other strings without special requirements should use double quotation marks. Double quotation marks are the most common use scenarios when defining variables.

7. Assign the result of the command to a variable

Shell also supports the assignment of command execution results to variables. There are two common ways:


In the first way, the command is surrounded by back quotation marks, which are very similar to single quotation marks, so it is not recommended to use this way; in the second way, the command is surrounded by back quotation marks$()Surrounded, the distinction is more obvious, so it is recommended to use this method.

For example, add two values to a variable.

varAdd1=`expr $var1 + $var2`
varAdd2=$(expr $var1 + $var2)

8. Read only variable

Use the readonly command, or use thedeclare -rortypeset -rA variable can be defined as a read-only variable, and its value cannot be changed.

readonly name

declare -r name="dablelv"

typeset -r name="dablelv"

9. Delete variables

Use the unset built-in command to delete variables. Grammar:

unset variable_name

The unset command cannot delete read-only variables. A variable cannot be reused after it has been deleted. as


unset name
echo $name

The script above has no output.

The above is the detailed content of shell variables, more information about shell variables, please pay attention to other related articles of developer!