In shell scripts, variables are divided into two types, system variables, and custom variables.
System default variables are some of the system's own variables, such as PATH variable
User-defined variables are some of the variables that you define when you write the script
Variable name naming rules
The first character must be the letter "A-Z and a-Z"
The middle cannot have spaces, but you can use the underscore "_"
Punctuation cannot be used
Cannot use keywords in bash
There can be no spaces between the variable name and the equals sign
Read-only variables
Use the readonly command when defining a variable as a read-only variable
Edit the file as follows:
The results are as follows:
Delete a variable
Delete using the unset command
Syntax: unset variable name
The variable cannot be used again after it is deleted, and unset cannot delete the read-only variable, and the value of the read-only variable cannot be changed
Using variables with $
In shell programming, so the variable is composed of strings, and does not need to declare the type of the variable beforehand, it supports the only type of variable is the string
When using a defined variable, simply add "$" to the variable name before
Edit the text as follows:
The results are as follows:
·
The curly braces outside the variable name are optional, plus the curly braces help to read the understanding and the interpreter is the boundary of the variable
For example, the variable is hello_name
echo "This is $hello _nameandtynam"
echo "This is ${hello_name}andtynam"
The first tries to hello_nameandtynam the most variable, and the second is to use Hello_name as the variable
Shell Programming-Variables (iii)