How the shell intercepts strings

Source: Internet
Author: User
Tags string methods

Summary of Shell interception string methods in Linux

How does [Linux] calculate string lengths in shell scripts?

There are eight ways to intercept a string, mainly for the following methods

There are a number of ways to intercept strings in the shell, ${expression} There are 9 ways to use them. ${parameter:-word}${parameter:=word}${parameter:?word}${parameter:+word} The above 4 alternatives can be used for default values. ${#parameter} This can get the length of the string. ${parameter%word} minimum intercept from the back Word${parameter%%word} to maximize the interception from the back Word${parameter#word} minimal interception from the front word${parameter# #word} The best way to intercept word above 4 is to intercept the string. With four usages, you don't have to use the cut command to intercept a string. The first type can be divided into four cases, described below. 1. Use the # operator. Use isDelete the first occurrence of the substring, which is the left character, starting from the left, preserving the right character. Usage is #*substr, for example: Str= ' http://www. your domain name. com/cut-string.html ' echo ${str#*//The result is www. your domain name. com/cut-string.html, which deletes all characters from the left to the first "//" and its left
2. Use the # # operator. The purpose is to delete the last occurrence of the substring, which is the left character, from the left side, leaving the right character. Usage is ##*substr, for example: Str= ' http://www. your domain name. com/cut-string.html ' echo ${str##*/The result is cut-string.html, which deletes the last occurrence of "/" and all of its left characters 3, using the% number operator. Use isDelete the first occurrence of the substring, which is the right character, starting from the right, leaving the character on the left. Usage is%substr*, for example: Str= ' http://www. your domain name. com/cut-string.html ' echo ${str%/*The result is http://www. your domain name. com, which deletes all characters from the right to the first "/" and its right 4, using the percent sign operator. Use isDelete the last occurrence of the substring, which is the right character, starting from the right, leaving the character on the left. Usage is%%substr*, for example: Str= ' http://www. your domain name. com/cut-string.html ' echo ${str%%/*The result is http://www. your domain name. com, that is, delete from the right to the last "/" and all the characters to the right of the second is also divided into four kinds, respectively, the following: 1, starting from the left of the first few characters and the number of characters, using: Start:len, for example: str= ' http://www. Your domain name. com/cut-string.html ' echo ${var:0:5} where 0 represents the beginning of the first character on the left and 5 represents the total number of characters. The result is: Http:2, starting from the first few characters on the left until the end, using: Start, for example: Str= ' http://www. your domain name. com/cut-string.html ' Echo ${var:7} of which 7 The 8th character on the left starts with the result: www. your domain name. COM/CUT-STRING.HTML3, starting with the first few characters on the right and the number of characters, usage: 0-start:len, for example: Str= ' http://www. your domain name. com/ Cut-string.html ' echo ${str:0-15:10} where 0-6 means the 6th character starts at the right, and 10 indicates the number of characters. The result is: Cut-string3, starting from the first few characters on the right to the end, using: 0-start, for example: Str= ' http://www. your domain name. com/cut-string.html ' Echo ${str:0-4} of which 0-6 Indicates that the 6th character starts at the right, and 10 indicates the number of characters. The result is: HTML Note: (the first character on the left is denoted by 0, and the first character on the right is denoted by 0-1)

How the shell intercepts 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.