Variable type
I always thought that the bash shell variables are non-typed and can also be specified.
Administrator@DADI ~$ declare -i num=10Administrator@DADI ~$ echo $num10Administrator@DADI ~$ num='iamaint'Administrator@DADI ~$ echo $num0
After the type is forcibly specified using the declare method, the variable does not receive non-
Int.
Constant definition Syntax:
readonly OPTION VARIABLE(s)
After definition, the variable cannot be re-assigned or unset
Administrator@DADI ~$ readonly MY_NAME='Administrator'Administrator@DADI ~$ MY_NAME='ANOTHERNAME'-bash: MY_NAME: readonly variableAdministrator@DADI ~$ unset MY_NAME-bash: unset: MY_NAME: cannot unset: readonly variable
Two definition methods for Array Operations
ARRAY=(value1 value2 ... valueN)ARRAY[0]="no.1 element"
Element
myarray=(1 2 3 4 5 6 7 8)echo ${myarray[0]}myarray[0]="new value 1"
Number of array elements
echo ${#myarray[*]}
Do not confuse with the following. Only the length of the first element of myarray is returned.
echo ${#myarray}
Traversal
echo ${myarray[*]}for i in ${myarray[*]} ; do echo $i;done
Continuous length can also be used
array_len=${#myarray[*]}i=1while [ $i -lt $array_len ]do echo ${myarray[$i]}; i=$[ $i + 1 ];donedone;
String operation substring
${VAR:OFFSET:LENGTH}
Administrator@DADI ~$ mystring="123456789"Administrator@DADI ~$Administrator@DADI ~$ echo ${mystring:2:2}34Administrator@DADI ~$ echo ${mystring:2:4}3456
Replace
${VAR/PATTERN/STRING}
$ echo ${mystring/2/new2}1new23456789