Shell variable advanced usage

Time:2021-6-13

Variable replacement and testing

Variable substitution

grammar explain
${variable name # matching rule}} Dependent variablestartRule matching will meet the most important requirementsshortData deletion for
${variable name # matching rule}} Dependent variablestartRule matching will meet the most important requirementslongData deletion for
${variable name% matching rule}} Dependent variabletailRule matching will meet the most important requirementsshortData deletion for
${variable name%% matching rule}} Dependent variabletailRule matching will meet the most important requirementslongData deletion for
${variable name / old string / new string}} If the content of the variable matches the old string, thenfirstThe old string is replaced by the new string
${variable name // old string / new string}} If the content of the variable matches the old string, thenall-outThe old string is replaced by the new string
[[email protected] ~]$ variable_1="I love you,Do you love me"
[[email protected] ~]$ echo $variable_1
I love you,Do you love me
[ [email protected]  ~]$ var1=${variable_ 1 # * ov} // starting from scratch, the shortest match
[[email protected] ~]$ echo $var1 
e you,Do you love me
[ [email protected]  ~]$ echo ${variable_ 1 # * ov} // start from scratch, longest match
e me
[ [email protected]  ~]$ echo ${variable_ 1% ov *} // starting from tail, shortest match
I love you,Do you l
[ [email protected]  ~]$ echo ${variable_ 1%% ov *} // starting from tail, longest match
I l

[email protected] ~]$ echo $PATH
/usr/local/php/bin:/usr/local/nginx/sbin:/usr/local/mysql/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/usr/local/go/bin:/usr/local/go/bin:/home/wonbin/.local/bin:/home/wonbin/bin:/home/wonbin/.config/composer/vendor/bin
[ [email protected]  ~]$echo ${path / bin / bin} // single replacement
/usr/local/php/BIN:/usr/local/nginx/sbin:/usr/local/mysql/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/usr/local/go/bin:/usr/local/go/bin:/home/wonbin/.local/bin:/home/wonbin/bin:/home/wonbin/.config/composer/vendor/bin
[ [email protected]  ~]$echo ${path // bin / bin} // replace all
/usr/local/php/BIN:/usr/local/nginx/sBIN:/usr/local/mysql/BIN:/usr/local/BIN:/usr/BIN:/usr/local/sBIN:/usr/sBIN:/usr/local/go/BIN:/usr/local/go/BIN:/home/wonBIN/.local/BIN:/home/wonBIN/BIN:/home/wonBIN/.config/composer/vendor/BIN

Variable test

Variable configuration mode STR is not configured STR is an empty string STR configured and not empty
var=${str-expr} var=expr var= var=$str

string manipulation

Calculate string length

grammar explain
Method 1 ${#string} nothing
Method 2 expr length "$string" String if there are spaces, you must use double quotation marks
[[email protected] ~]$ echo ${#var_str}
11
[[email protected] ~]$ echo `expr length "$var_str"`
11

Gets the position of the substring index in the string

grammar explain
expr index $string $substring Find any character in the substring, find the position of the first character and return

Find character  u  or  x  The position of (which letter appears first, calculate which one)::

[[email protected] ~]$ string="everything is just beginning"
[[email protected] ~]$ echo `expr index "$string" ux`
16

Calculation of substring length

grammar explain
expr match $string $substring The left side of the string matches from the beginning,
[[email protected] ~]$ string="did you do exercise today ?"
[[email protected] ~]$ echo `expr match "$string" do`
0
[[email protected] ~]$ echo `expr match "$string" "did you"`
7
[[email protected] ~]$ echo `expr match "$string" did.*`
27

be carefulexpr indexandexpr matchIt’s not a lot in practice

Extract string

grammar explain
Method 1 ${string:position} Starting from position position in string, index subscript starts from 0
Method 2 ${string:position:length} Starting from position, the matching length is length
Method 3 ${string: -position} Match from the right, paying attention to the space after the colon
Method 4 ${string:(position} Match from the left
Method 5 expr substr $string $position $length Starting from position, the matching length is length, and the index subscript starts from 1
[[email protected] ~]$ var_str="kubernetes docker cloudNative  promethus"[[email protected] ~]$ echo ${var_str: 3}
ernetes docker cloudNative promethus
[ [email protected]  ~]$ echo ${var_ str:3 : 5} // method 2, start position 3, length 5
ernet
[ [email protected]  ~]$ echo ${var_ STR: - 5} // method 3, the colon must be followed by a space
ethus
[ [email protected]  ~]$ echo ${var_ STR: - 5:3} // method 3, band length
eth
[ [email protected]  ~]$ echo ${var_ str:-5 }// method 3: the colon is invalid without spaces
kubernetes docker cloudNative promethus
[ [email protected]  ~]$ echo ${var_ STR: (- 5)} // method 4. If there is no space after the colon, you can use parentheses
ethus
[ [email protected]  ~]$ echo `expr substr "$var_ STR "1 5 '// method 5, the index counts from 1
kuber

This work adoptsCC agreementReprint must indicate the author and the link of this article