Linux Shell interception string

Many methods string shell intercepts

Copy the code
was $ {# * /} 
$ {were ## * /} 
$ {var% / *} 
$ {were %% / *} 
$ {were: start: len} 
$ {were: starting} 
$ {were: 0- start: len} 
$ {var: 0 start}
Copy the code

 


The following example shows what a few:

1) obtaining a length of the string

grammar:

$ # {Was}

 

Sample code:

str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]"

length=${#str}
echo "length : [${length}]"

 

Results of the:

string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
length : [61]

 


2) acquired using a # ## and trailing substring

2.1) # minimum word taken from the front

grammar:

${parameter#word}  

 

Sample code:

Copy the code
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]"

#分割符为'/'
substr=${str#*/}
echo "substr : [${substr}]"
Copy the code

 

Results of the:

string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [/www.fengbohello.xin3e.com/blog/shell-truncating-string]

 

2.2) ## maximum word taken from the front

grammar:

${parameter##word}

 

Sample code:

Copy the code
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]"

#分割符为'/'
substr=${str##*/}
echo "substr : [${substr}]"
Copy the code

 

Results of the:

string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [shell-truncating-string]

 


3)%%% Get the head and the substrings

3.1)% minimum word taken from behind

grammar:

${parameter%word} 

 

Sample code:

str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]"

substr=${str%/*}
echo "substr : [${substr}]"

 

Results of the:

string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [http://www.fengbohello.xin3e.com/blog]

 

3.2) maximizing %% word taken from behind

grammar:

${parameter%%word}

 

Sample code:

str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]"

substr=${str%%/*}
echo "substr : [${substr}]"

 

Results of the:

string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [http:]

 


4) $ {var:} mode acquiring substrings

4.1) Specifies the number of characters from the left of the first few characters of the start and substring

grammar:

$ {Var start: len}

 

Sample code:

Copy the code
= STR "http://www.fengbohello.xin3e.com/blog/shell-truncating-string" 
echo "String: [STR $ {}]" 

# 0 where the left side represents the first character, the sub-character 7 denotes of the total number. 
STR $ {= substr: 0:. 7} 
echo "substr: [$ {substr}]"
Copy the code

 

Results of the:

string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [http://]

 

4.2) from the left of the first few characters until the end

grammar:

$ {Var: 7}

 

Sample code:

Copy the code
= STR "http://www.fengbohello.xin3e.com/blog/shell-truncating-string" 
echo "String: [STR $ {}]" 

# 7 which represents the left of the first eight characters beginning 
substr = $ {str :. 7} 
echo "substr: [$ {substr}]"
Copy the code

 

Results of the:

string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [www.fengbohello.xin3e.com/blog/shell-truncating-string]

 

4.3) from the right side of the first few characters of the number and character of the beginning

grammar:

$ {Var: 0 start: len}

 

Sample code:

Copy the code
= STR "http://www.fengbohello.xin3e.com/blog/shell-truncating-string" 
echo "String: [STR $ {}]" 

# 0-23 where the right side represents the start date of 23 characters, 5 showing the number of characters 
substr = $ {STR: 0-23: 5} 
echo "substr: [$ {substr}]"
Copy the code

 

Results of the:

string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [shell]

 

4.4) starting from the right of the first few characters until the end

grammar:

Was $ {0} boot

 

Sample code:

Copy the code
= STR "http://www.fengbohello.xin3e.com/blog/shell-truncating-string" 
echo "String: [STR $ {}]" 

# 0-6 where the right side represents the sixth character starts counting 
substr STR $ {=: 0-6} 
echo "substr: [$ {substr}]"
Copy the code

 

Results of the:

string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [string]

Guess you like

Origin www.cnblogs.com/kakaisgood/p/11375570.html