Linux Bash Shell string truncation, bashshell

Source: Internet
Author: User

Linux Bash Shell string truncation, bashshell

#! /Bin/bash

# No space is allowed on both sides of the equal sign when the variable is assigned a value. Otherwise, the command does not exist.

# Run shell scripts in two ways
#1. As the interpretation parameter/bin/sh test. sh; 2. As the executable file chmod + x./test. sh
Blog_url = 'HTTP: // www.cnblogs.com/big#/p/7535900.html'
#1. # intercept the number, and delete the part starting from 0 to the First Matching condition ('//') from left to right. Retain the remaining part.
Echo $ {blog_url #*//}
# The running result is as follows: www.cnblogs.com/big#/p/7535900.html

#2, ## # truncate, delete from left to right from 0 to the last matching condition ('/'), and retain the remaining part
Echo $ {blog_url ##*/}
# The running result is as follows: 7535900.html

#3. truncate "%", and delete the part from # blog_url to the First Matching condition ('/') from the right to the left, and retain the remaining part.
Echo $ {blog_url % /*}
# Run the result of the following http://www.cnblogs.com/Bighua/p

#4. Intercept the % number, and delete the part starting from # blog_url to the last matching condition ('/') from the right to the left. The remaining part is retained.
Echo $ {blog_url % /*}
# The running result is as follows:

#5. The number of characters on the left
Echo $ {blog_url: 0: 7}
# The running result is as follows: http ://

#6. Start from the number of characters on the left to the end
Echo $ {blog_url: 7}
# The running result is as follows: www.cnblogs.com/big#/p/7535900.html

#7. The number of characters starting from the right is used as the starting point, and the number of characters from left to right is truncated.
Echo $ {blog_url: 0-7:2}
# The running result is as follows: 00

#8. Start from the right to the end
Echo $ {blog_url: 0-4}
# Running result html

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.