1, the variables in the shell can be customized, the shell uses the variable with the $
Name= "Shero"
echo "Hi ${name}"
[Email protected]:/home/shero/shell#./test2.sh
Hi Shero
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 the keywords in bash (you can see the reserved keywords using the help command).
- It is a good programming habit to add curly braces to all variables.
- The dollar symbol ($) is used when using variables.
- Variable name does not add dollar sign ($) when defining a variable
2. Delete variables
unset variable Name
eg
Name= "Shero"
Firstname= "Huo"
echo "Hi $name $FirstName"
unset name
echo "Hi $name $FirstName"
Execution Result:
[Email protected]:/home/shero/shell#./test2.sh
Hi Shero Huo
Hi Huo
3. Variables can be redefined:
Name= "Shero"
Firstname= "Huo"
echo "Hi $name $FirstName"
#unset Name
#echo "Hi $name $FirstName"
Name= "Lucy"
echo "Hi $name"
Execution Result:
[Email protected]:/home/shero/shell#./test2.sh
Hi Shero Huo
Hi, Lucy.
4. Variables can be set as read-only variables
Name= "Shero"
Firstname= "Huo"
echo "Hi $name $FirstName"
#unset Name
#echo "Hi $name $FirstName"
ReadOnly Name
Name= "Lucy"
Firstname= "Lee"
echo "Hi ${name} ${firstname}"
Execution Result:
[Email protected]:/home/shero/shell#./test2.sh
Hi Shero Huo
./test2.sh:line 7:name:readonly Variable
Hi Shero Lee
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 Learning Notes (1)-Variables