Delete/replace of substring of shell variable

Source: Internet
Author: User
Tags ming

${#string}
Returns the length of the $string

${string:position}
In $string, start extracting substrings from the $position position

${string:position:length}
In $string, the $length-length substring is extracted from the $position position

[Root@localhost shell]# name= "Ni hao, Ming tian"
[root@localhost shell]# echo $NAME
ni hao, Ming tian
[root@l Ocalhost shell]# Echo ${name}
ni hao, Ming tian
[root@localhost shell]# echo ${#NAME}
[ Root@localhost shell]# Echo ${name:3}
Hao, Ming Tian
[root@localhost shell]# Echo ${name:3:6}
Hao, M
[Root@localhost shell]# 

${string#substring}
Delete the shortest match $substring substring starting at the beginning of the variable $string

${string# #substring}
Delete the longest matched $sunstring substring starting at the beginning of the variable $string

e
[root@localhost shell]# echo $NAME
abcabc123abcabcde
[root@localhost shell]# Echo ${name#a*c}
123ABCABCDE
[root@localhost shell]# echo ${name# #a *c}
ABCDE

${string%substring}
Delete the shortest match $substring substring starting at the end of the variable $string

${string%%substring}
Deletes the longest matched $substring substring starting at the end of the variable $string

[Root@localhost shell]# name=abcabc123abcabc
[root@localhost shell]# echo $NAME
abcabc123abcabc
[ Root@localhost shell]# Echo ${name%%a*c}

[Root@localhost shell]# Echo ${name%a*c}
abcabc123abc
[ Root@localhost shell]# Echo ${name#b*c}
abcabc123abcabc
[root@localhost shell]# Echo ${NAME#BC}
ABCABC123ABCABC
[Root@localhost shell]# Echo ${NAME%BC}
abcabc123abca
[root@localhost shell]# Echo ${ NAME%%BC}
abcabc123abca
[root@localhost shell]# Echo ${name%c*c}
abcabc123ab
[root@localhost shell]# Echo ${name%%c*c}
abcab
[root@localhost shell]# Echo ${name%%c*b}
abcabc123abcabc
[ Root@localhost shell]# Echo ${name}
abcabc123abcabc

Note: In the # or # #匹配时, the first character of the $string must be the first character of the deleted substring $substring, or the deletion cannot be matched;
in the case of a% or Percent match, the last character of the $string must be the last character of the deleted substring $substring, or the delete operation cannot be performed;

${parameter/parttern/string}
Replace the first matching pattern with a string

${parameter/#parttern/string}
Matches the pattern in the parameter variable from the beginning, and then uses string to replace the matching pattern.

${parameter/%pattern/string}
Matches the pattern in the parameter variable from the end, replacing the matching pattern with string after matching

${parameter//pattern/string}
Replace all matching pattern in the parameter variable with string

[Root@localhost shell]# name= "I am a student student"
[Root@localhost shell]# Echo ${name//student/teacher}
I am A teacher teacher
[root@localhost shell]# echo ${name/#student/teacher}
I am a student student
[ Root@localhost shell]# Echo ${name/%student/teacher}
I am a student teacher
[root@localhost shell]# Echo ${name/  %I Am/teacher}
I am a student student
[root@localhost shell]# echo ${name/#I am/teacher}
teachera Student Student
[root@localhost shell]# echo ${name/#I am A/teacher}
teacher student student
[root@localhost shell]# Echo $NAME
I am a student student
[Root@localhost shell]# 
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.