Multiple ways to intercept shell script strings

Source: Internet
Author: User


The first four methods are to remove some characters, and the last four methods to preserve certain characters


1, # Intercept, delete the left character, retain the right character

var= "Hello World"
Echo ${var#*l}
Results:
Lo World

where Var is the variable name and the # is an operator, *l means that all characters from the first L and the left are deleted from the left.
that is, delete hel


2. # # Intercept, delete the left character, retain the right character.

var= "Hello World"
Echo ${var##*l}
Results:
D

##*l indicates that the last (rightmost) one L and all characters on the left are deleted from the left.
That is, delete Hello Worl


3,% number intercept, delete the right character, leave the left character

var= "Hello World"
Echo ${var%l*}
Results:
Hello Wor

%l* indicates that the first L and the right character are deleted from the right.

That is, delete D


4, the percent of the interception, delete the right character, the left character remains

var= "Hello World"
Echo ${var%%l*}
Results:
He

%%l* indicates that the last (leftmost) one L and right character is deleted from the right.

That is, delete Llo world


5, starting from the first few characters on the left, and the number of characters

var= "Hello World"
Echo ${var:0:3}
Results:
Hel

0 represents the first character on the left and 3 indicates the total number of characters.


6, starting from the first few characters on the left, until the end.

var= "Hello World"
Echo ${var:2}
Results:
Llo World

2 means that the 3rd character on the left begins, until the end.


7, starting from the right of the first few characters, and the number of characters

var= "Hello World"
Echo ${var:0-2:3}
Results:
Ld

0-2 means the second character starts at the right, and 3 indicates the number of characters.


8, starting from the first few characters on the right, until the end.

var= "Hello World"
Echo ${var:0-2}
Results:
Ld

Indicates that the second character from the right begins, until the end.




1 #!/bin/bash 2 var= "Hello World" 3 Echo ${var#*l} 4 echo ${var##*l} 5 echo ${var%l*} 6 echo ${var%%l*} 7 echo ${var : 0:3} 8 echo ${var:2} 9 echo ${var:0-2:3} Ten echo ${var:0-2}[[email protected] test6_16]$./string.sh Lo Worlddhello wor Hehelllo WORLDLDLD


This article is from the "Small Town Moss" blog, please make sure to keep this source http://fengbaoli.blog.51cto.com/10538178/1790049

Multiple ways to intercept shell script strings

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.