Shell processing strings and {} operations
1. Get the length of a variable
There are about 4 ways to get the length of a string: (in the case of Hello World)
Passwc -L
$ echo "hello world" | wc -L 11
Passexpr length string
$ expr length "hello world"11
The length function built through awk
$ echo "hello world" | awk ‘{print length($0)}‘11
Passecho ${#string_name}
$ string=‘hello world‘$ echo ${#string}11
2. Intercept variable by {} action
#
Operator--from the leftmost start to the first occurrence of a substring, delete all
$echo $stringhello world$echo ${string#*l}lo world$echo ${string#*ll}o world$echo ${string#*llo}world$echo ${string#llo}hello world
Note: this needs to be matched with a *, otherwise it will be as invalid as the last one, and the first space in the penultimate part is removed, this should be a default assignment for the shell, which will remove the space at the beginning of the variable. For example:
$str1=" hello"$echo $str1hello
##
is from the leftmost match to the last occurrence of the substring.
$echo ${string##*l}d
%
From the far right to the first occurrence of the substring, the * number is placed to the right of the matched substring
$echo ${string%l*}hello wor
%%
is from the far right to the place where the substring last appears.
$echo ${string%%l*}he
${string:m:n}
Operation
Attention:
The first subscript on the left is denoted by 0.
The first table on the right is denoted by 0-1, the second on the right is 0-2, and so on, n indicates the number of characters, not subscript
${string:3}
3 here means starting from the 4th on the left.
$echo $stringhello world$echo ${string:0:2}he$echo ${string:0-2:2}ld$echo ${string:2}llo world
3. Parameter substitution and extension--by {} action
${str-value}
If STR is not declared, value is assigned
${str:-value}
If STR is not declared or the value is NULL, assign value
$var=$echo ${var-helo} -- 此时变量var为空$echo ${var:-helo}helo
${str+value} use value as the default value regardless of str value
${str:+value} use value as the default value unless str is empty
${str=value} If STR is not declared, use value as the default value, and STR is assigned value
${str:=value} If STR is not declared, or is empty, value is used as the default value and STR is assigned as value
${str?value} If STR is not declared, output value to stderr
${str:?value} If STR is not declared, or is empty, output value to stderr
${!var}
Indirect variable Reference
$echo $stringhello world$str="string"$echo ${!str}hello world
${!prefix*}
Match all previously declared variables with prefix
${[email protected]}
Match all previously declared variables with prefix
$cat test.sh c_var1="hello"c_var2="world"for c in ${!c_*}; do echo "${c}=${!c}"done$sh test.sh c_var1=helloc_var2=world
4. String substitution--by {} action
${string/str1/str2}
, replace with str2 the first str1 from the left number
${string//str1/str2}
, replace all str1 with str2
${string/#str1/str2}
If the string prefix is str1, replace it with STR2
${string/%str1/str2}
If the suffix of string is str1, replace it with STR2
$echo ${string/l/s}heslo world$echo ${string//l/s}hesso worsd$echo ${string/#he/h}hllo world$echo ${string/%ld/d}hello word
Shell processing strings and {} actions