shell ${}

 

$ {...} has many uses in strings:

1, $ {was}

Limit variables.

If a variable name A is the front part of another variable name AB, then if you want to get the value of A when AB is connected, you must use the $ {var} qualification.

If there is no ambiguity in variable names (that is, independent variable names), there is no difference between $ {var} and $ var.

var="hello"

var_01="hello01"

echo $ var $ var_01 $ {var} _01

>>> hello hello01 hello_01

 

2, # $ {was}

Get the length of the variable var

var="hello"

echo $ {# var}

>>> 5

 

3, $ {var #}

Delete the character (string) at the head of the variable,

"#" Can be followed by a literal string or a regular expression (lazy matching).

var="hello"

echo $ {var # he}

>>> llo

var="/res_pos/path/from/something"

echo $ {var # /}

>>> res_pos / path / from / something # delete the first "/"

echo $ {var # / * /}

>>> path / from / something # Delete the first string matching "/ * /", that is, "/ res_pos /"

echo $ {var # res}

>>> / res_pos / path / from / something # res is not in the header, it will not be deleted

 

4, was $ {##}

Delete the character (string) at the head of the variable,

However, when using two "##", the matching is greedy (greedy matching, that is, as many matches as possible).

echo $ {var ## / * /}

>>> something # Delete the first string that matches "/ * /", that is, "/ res_pos / path / from /".

 

5, $ {%} was

Delete the character (string) at the end of the variable,

"%" Can be a literal string or a regular expression (lazy matching).

var="hello"

echo $ {var% llo}

>>> he

var="~/res_pos/path/from/something"

echo $ {var% / *}

>>> ~ / res_pos / path / from # delete the trailing "/ something"

 

6, $ {var %%}

Delete the character (string) at the end of the variable,

Only, when using 2 "%%", the match is greedy (corresponding to Article 4)

var="~/res_pos/path/from/something"

echo $ {var% / *}

>>> ~ # Delete everything after the first "/" (including "/"). In greedy matching, "/ *" is all strings after "/"

 

7, $ {was} ::

Similar to Python's slicing concept, take the starting index of the variable var,

Substring of specified length ($ {var: index: length}).

var="~/res_pos/path/from/something"

echo $ {var: 0: 5}

>>> ~/res

echo $ {var: 1: 5}

>>> / res_ # Visible, the index of the string starts from 0

echo $ {var :: 5}

>>> ~ / res # Omit "index", the default is to start from 0

echo $ {var: 1:}

>>> / res_pos / path / from / something # Omit "length", the default length is all

 

 

 

8, was $ {//}

String replacement.

Replace the string A in var with the string B ($ {var / A / B}).

var="hello world"

echo $ {var / hello / hi}

>>> hi world # The string "hello" is replaced with "hi"

echo ${var/l/X}

>>> heXlo world # Replace the first "l" with "X"

 

9, $ {was} ///

String replacement,

Same as point 8, except that now all occurrences of string A are replaced with string B ($ {var // A / B}).

var="hello world"

echo ${var//l/X}

>>> heXXo worXd # All "l" are replaced with "X"


10, $ {var / # /}

Replace string A at the head of the string with string B,

($ {Var / # A / B}), unlike point 8, only the first string A is replaced here.

var="hello world. hello baby."

echo ${var//h/H}

>>> Hello world. Hello baby. # Replace all "h" with "H"

echo ${var/#h/H}

>>> Hello world. Hello baby. # Replace "h" in the header with "H"

echo $ {var / e / E}

>>> hEllo world. hello baby. # Replace the first occurrence of "e" with "E"

echo $ {var / # e / E}

>>> hello world. hello baby. # The first character is not "e", so there is no replacement, and it is output as it is.

 

11, was $ {/% /}

Replace string A at the end of the string with string B,

($ {Var /% A / B}), similar to point 10, except that this deals with the end of the string.

var="hello hello"

echo $ {var /% o / END}

>>> hello hellEND # Replace the trailing character "o" with "END".
————————————————
Copyright Statement: This article is an original article by CSDN blogger "Lao Like", which follows the CC 4.0 BY-SA copyright agreement. And this statement.
Original link: https://blog.csdn.net/lihonghai2392/article/details/77868445

Guess you like

Origin www.cnblogs.com/yangxinrui/p/12703765.html