First, define variables
Define variable names without a $ (unlike PHP, as with Python), with no spaces between variable names and "=" (special, unlike other languages)
Example: x= "Hello World"
Note: The naming of 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).
Referencing a defined variable requires the addition of a $ character
Such as:
x= "Hello World"
Echo ${x}
Second, read-only variables
Defined with the ReadOnly command, such as:
x= "Hello World"
ReadOnly x
The value of x cannot be changed at this time
Third, delete the variable
Use the unset command to delete a variable, but the unset command cannot delete a read-only variable
Iv. getting the string length
string= "ABCD"
echo ${#string} #输出 4
Five, find sub-characters
String="Runoob is a great company"
echo ' expr index ' $string ' is ' # output 8
Six, extracting substrings
The following instance intercepts 4 characters starting with the 2nd character of the string:
String="Runoob is a great site"
echo ${string:1:4} # output Unoo
Seven, reading the array
Valuen=${array_name[n]}
Read all the elements in the array:
Use the @ sign, echo ${array_name[@]}
Shell Programming--variables