One, variable
Variables in the shell are divided into: local variables, environment variables, positional parameters;
Local variables: Variables that can only be used in scripts for the user's current shell lifetime, local variables are invalidated as the shell process dies, local variables are still invalid in the newly started shell, similar to the concept of local variables in C and C + +;
Environment variables: Applies to all the processes generated by the login process;
Positional parameters: Used to provide pass parameters to the shell script, which is read-only;
A variable is the name of a value, the value of a reference variable is called: variable substitution, the $ symbol is a variable substitution symbol, such as variable is the variable name, then $variable is the value of the expression variable;
Variable=value #切记: Spaces cannot appear on either side of the equals sign
${variable=value} #同上
Examples are as follows:
var1= "Hello World"
Var2= "I say"
Var3= "We are saying $var 1" #ok
Purge of variables: unset command
unset variable Name
unset var, echo $var, the result shows a blank line, which means that the Var variable is not initialized;
Variable Assignment mode:
Var=value #注意: cannot have spaces on either side of the equals sign
1 1, variable initialized condition 2[Email protected]222- the- -- -:~/CP/SH# colour="Reb"3[Email protected]222- the- -- -:~/CP/SH#Echo "is ${colour+"Blue"}"4 is Blue5[Email protected]222- the- -- -:~/CP/SH#Echo$colour6 Reb7[Email protected]222- the- -- -:~/CP/SH# unset Colour82, the variable is not initialized, ": =" and ":-" for the initialized variable operation, no effect 9[Email protected]222- the- -- -:~/CP/SH# unset ColourTen[Email protected]222- the- -- -:~/CP/SH#Echo$colour One A[Email protected]222- the- -- -:~/CP/SH#Echo "is ${colour:="Red"}" - is Red -[Email protected]222- the- -- -:~/CP/SH#Echo$colour the Red -[Email protected]222- the- -- -:~/CP/SH# unset Colour -[Email protected]222- the- -- -:~/CP/SH#Echo "is ${colour:-"Red"}" - is Red +[Email protected]222- the- -- -:~/CP/SH#Echo$colour - +[Email protected]222- the- -- -:~/CP/SH#
Fundamentals of shell Programming for Linux