The Shell operator.

  • shellThe variable in is string type by default and weakly typed.

declare

declareThe command

  • Declare declares the type of a variable.

  • Declare [+/-] [Options] Variable name

options meaning
- Assign type attributes to variables
+ Undeclare the type of the variable
-a Declare the variable as an array
-l Declare the variable as an integer
-i Declare the variable as an integer
-x Declare the variable as an environment variable (equivalentExport [variable name]=[variable value])
-r Declare a variable as read-only
-p Displays the type identifier of a variable

Demo

C =$a+$b echo $c # 1+2 declare -i c=$a+$c # echo $c # 3 Declare + I c # echo $c # 1+2 declare -c =$a+$b echo $c # 1+2 declare -c =$a+$b echo $c # 1+2 Declare -i declare $d = 1 - p $c # declare -i declare environment variable $c = "3" # declare - x $e = 6 set | grep e = 10 # e = 10 env | grep e = 10 # e = 10  declare -r x=10 x=11 # bash:x is readonlyCopy the code

An array of

  • throughDeclare -a [variable name]You can declare a variable of type array.

Delcare -a names # declare an array variable names

names[0]=wang
names[1]=haoyu
Copy the code

When calling echo [array type], only the first array is printed by default:

If you need extra attention, you need to use ${array} when calling an array type. Because the array type representation is a computable whole.

echo ${names} # wang

echo ${names[1]} # haoyu
Copy the code

Echo $names[1] echo $names[1] echo $names[1] echo $names[1] echo $names[1] echo $names[1] You need to use ${names[1]} to get the correct result.

  • through*Wildcards print all the contents of an array.

echo ${names[*]} # wang haoyu

The environment variable

Declare an environment variable xx with a value of 6 by declaring -x xx=6.

It has the same effect as export xx=6: declare environment variable statements; declare -x is what export calls internally.

Read-only property

Declare a read-only variable with declare -r [variable].

Query variable Attributes

Declare -p [variable] to view the type properties of a variable.

The method of numerical operation of variables

  • As long as through thedeclareDeclaration of the variable type, can beThe numericalI’m going to do that.

exprAs well aslet

The calculation of variable types can be performed with the EXPr and let keywords.

Sum =$(expr $num1 + $num2) $(expr $num1 + $num2) $(expr $num1 + $num2Copy the code
$($num1 + $num2)) echo sum2 # 3Copy the code
Sum3 =$[$num1 + $num2] echo sum3 # 3Copy the code