1.Shell variable 1.1 definition variable
Your_name= "Http://www.cnblogs.com/uniquefu"
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 name can only use English letters, numbers and underscores, the first character cannot begin with a number;
- Cannot have spaces in the middle, you can use the underscore (_);
- punctuation cannot be used;
- You can't use the keywords in bash (you can see the reserved keywords using the help command).
1.2 Using variables
With a defined variable, just precede the variable name with a dollar sign, such as:
Your_name= "Http://www.cnblogs.com/uniquefu" Echo ${your_name}echo $your _name
The curly braces outside the variable name are optional and add no lines, and curly braces are used to help the interpreter identify the bounds of the variable.
It's a good programming habit to add curly braces to all the variables.
Operation Result:
[Email protected] bin]#/test.sh Http://www.cnblogs.com/uniquefuhttp://www.cnblogs.com/uniquefu
Note: To assign a value to a variable, you cannot add the dollar sign, which is $your_name= "Uniquefu"
1.3 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 example attempts to change a read-only variable, resulting in an error:
Your_name= "Http://www.cnblogs.com/uniquefu" readonly your_nameyour_name= "https://www.baidu.com"
Operation Result:
[Email protected] bin]#/test.sh /test.sh:line 7:your_name:readonly variable
1.4 Deleting 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. unset command cannot delete read-only variables
Instance:
Your_name= "Http://www.cnblogs.com/uniquefu" unset Your_nameecho ${your_name}
The above instance execution will not have any output, and will not error
Shell Tutorial Variables