Array
Concept
A pair of parentheses indicates an array, and the elements of the array are separated by a "space" symbol in the form: array= (value0 value1 value2 value3 ...), when the subscript of the array defaults to 0
Basic operations
1.arr= (1 2 3 4 5)
2.arr= (
1
2
3
)
3.echo ${#arr [@]},${#arr [*]} #获取数组长度, with ${#数组名 [@ or *]} to get the array length
4.echo ${arr[0]},${arr[1]},${arr[2]} #获取数组元素的值
5.echo ${arr[*]} ${arr[@]} #获取数组所有的值, with the ${array name [subscript]} subscript is starting from 0 subscript is: * or @ Get the entire array contents
6.arr[1]=100 #给数组元素赋值, if the subscript does not exist, automatically adds a new array element
7.unset Arr[1] #删除数组中的某个元素
8.unset arr #删除整个数组
8.echo ${arr[@]:0:2} #数组切片, directly through the ${array name [@ or *]: Start position: Length} Slice the original array, return is a string, the middle with "space" separated
9.arr_new= (${arr[@]:0:2}) #数组切片, directly through the ${array name [@ or *]: Start position: Length} Slice the original array, return is a string, the middle with "space" separated, so if you add "()", will get the slice array, return is a new array
10.echo ${ARR[@]/1/11} #${array name [@ or *]/find character/replace character} This action does not change the original array contents, and if you need to modify it, you can redefine the data
11.for value in ${arr[*]};
Do
echo Value
Done
String
Defined:
${var} variable var value, same as $var
${var-default} If Var is not declared, then it takes $default as its value, and if the variable var is already set, then its value is $var
${var:-default} If Var is not declared, or its value is empty, then $default is used as its value, and if the variable var is already set, then its value is $var
${var=default} If Var is not declared, then it takes $default as its value, and if the variable var is already set, then its value is $var
${var:=default} If Var is not declared, or its value is empty, then $default is used as its value, and if the variable var is already set, then its value is $var
${var+other} If Var is declared, then its value is $other, otherwise it will be a null string
${var:+other} If Var is set, then its value is $other, otherwise it will be a null string
${var? Err_msg} If Var is not declared, then print $err_msg, if the variable var is already set, then its value is $var
${var:? Err_msg} If Var is not set, then print $err_msg, if the variable var is already set, then its value is $var
${!varprefix*} matches all variables declared at the beginning of Varprefix
${[email protected]} matches all previous variables declared at the beginning of the Varprefix
For example
1.echo ${abc-' OK '}
2.echo ${abc= ' OK '}
Basic operations
1.echo ${#string} #获取字符串的长度, ${#变量名} get string length
2.echo ${string:5},${string:5:10},${string} #字符串切片, ${variable name: start: length} get substring
3.echo ${string#*/} #${variable name #substring regular expression} from the beginning of the string with substring, delete the expression on the match
4.echo ${string%/*} #${variable name%substring regular expression} start with substring from the end of the string and delete the expression on the match
5.echo ${test//\//\\} #${variable/find/Replace value} One "/" means replacing the first, "//" means replacing all, when the lookup appears: "/" Please add escape character "/" to indicate.
Array of shell programming, basic manipulation of strings