Variable
Variables in Linux are divided into local variables, environment variables, position variables
Local variable: A local variable similar to C that does not exist in the newly started shell and is only defined in the current shell before it can be referenced
Environment variables: Applies to all the sub-processes generated by the login process, in short, the environment variables after the user logs on to all editors before the logoff, scripts and programs are valid
Positional parameters: Also variables, which are used to pass parameters to the shell script, are read-only (visible, shell scripts are also values)
Assigning values to variables
If the value of the variable has a space, you must add "", so here we emphasize that when referencing a variable, it is best to add "", for example, when crontab is executing in our system, if not "", the reference variable cannot be used
varibale= "Value" "$variable"
Crontab job examples to be added
Here we introduce let, which can be used for integer arithmetic
#!/bin/bash
a=2009
Let "A+=1"
echo "A= $a"
Environment variables
Position parameters
is a special shell variable that is used to pass parameters to the shell script from the command line, representing the first argument, and the 2nd one: $ A represents the script name, $* and [email protected], which represent all parameters, $ #表示参数个数
$$ script Run process number, $? Command exit status, 0 means no error, 0 indicates error
Reference
References refer to strings that are caused by reference symbols to prevent special characters from being re-interpreted by shell scripts as other meanings. For example:
LS * # no reference, * is interpreted as a wildcard character
LS "A *" # quoted, * is interpreted as literal meaning
"" Double quotation marks
You can refer to all characters except $ ' \ Three, which means that these 3 are also of special significance within double quotes. (Partial references)
For example, "$variable" means replacing variable names with variable values, and using double quotes to refer to variables prevents string splits and preserves spaces in variables, such as:
#!/bin/bash
variable=2010
echo "$variable" #显示结果 2010
Echo $variable #显示结果 2010
variable2= "x y z"
echo "$variable 2" #显示结果 X Y Z
echo $variable 2 #显示结果 x y Z, obviously, this is not what we want.
' Single quotes,
Single quotation marks can refer to all characters, that is, all characters are literal in single quotation marks, no special values, and therefore become full-referenced
Command substitution
Command substitution refers to assigning the standard output of a command to a variable as a value, and the bash shell defines two syntaxes for command substitution, one is ", the other is $ ()
For example:
Echo ' Who '
Dirlist= ' ls-l A * ' #命令替换
echo $dirlist #因为没加 "", so the newline character is deleted and the result is a 1-line string
echo "$dirlist" #给出的结果是多行字符串
Although the $ () and the ' number are equivalent on command substitution, the $ () Form of command substitution can be nested (in addition, this form is commonly used when doing mathematical operations)
firstlinelength=$ (expr Length "$ (sed-n ' 1p ' input)")
#!/bin/bash
Variable= "() \\{}\$\" "
echo $variable #显示结果 () \{}$ "
echo "$variable" #显示结果 () \{}$ "
Exit 0
06 Variables and references