The shell supports custom variables.
Defining variables
When defining a variable, the variable name does not have a dollar sign ($), such as:
Value= "TMP"
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 must 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 Bash's
- Keyword (use the help command to view reserved keywords).
Using variables
With a defined variable, just precede the variable name with a dollar sign ($), such as:
Your_name="Linux"echo $your _nameecho ${your_name}
The curly braces outside the variable name are optional, plus the curly braces are used to help the interpreter identify the bounds of the variable, such as the following:
for inch Ada coffe Action Java Do 'I am good at ${skill}script' done
If you do not add curly braces to the skill variable and write the 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.
Redefining variables
Defined variables, redefined
Myurl="http://see.xidian.edu.cn/cpp/linux/"echo ${myurl}myurl=" http://see.xidian.edu.cn/cpp/shell/ " Echo ${myurl}
Note the second assignment cannot be written $myurl= "http://see.xidian.edu.cn/cpp/shell/"
Read-only variables
Use the readonly command to define a variable as a read-only variable, and the value of a read-only variable cannot be changed
The following variable is a read-only variable, which results in error execution
#!/bin/Bashmyurl="http://see.xidian.edu.cn/cpp/shell/"readonly Myurlmyurl="http://see.xidian.edu.cn/cpp/danpianji/"
The result of running the script is as follows:
/bin/sh:name:this variable is read only.
Delete a variable
Use the unset command to delete a variable. Grammar:
- Unset variable_name
The variable cannot be used again after it has been deleted; The unset command cannot delete a read-only variable.
As an example:
#!/bin/Shmyurl="http://see.xidian.edu.cn/cpp/u/xitong/"unset Myurlecho $ Myurl
The script above does not have any output
Variable type
When you run the shell, there are three different variables:
1) Local Variables
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.
2) Environment 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.
3) Shell variables
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.
Shell Programming-Variables