Common shell string operations

Source: Internet
Author: User

1. String Truncation

(1) intercept the string before and after a specified character

[Root @ gd86 applications] # Var = Hello: zkl: zyh

[Root @ gd86 applications] # echo $ {var %: *} returns the first string from right to left.
Hello: zkl
[Root @ gd86 applications] # echo $ {var %: *} returns the last string from the right to the left.
Hello
[Root @ gd86 applications] # echo $ {var # *:} count from left to right and capture the last string:
Zyh
[Root @ gd86 applications] # echo $ {var # *:} returns the first string after the first string.
Zkl: zyh

(2) truncate a string at the specified start position and length

[Root @ gd86 applications] # echo $ {var: 0: 5} intercepts a string starting from string 0 with a length of 5

Hello

Or

Expr substr $ var 1 2 truncates a string starting from position 1 with a length of 2. The first string position is 1. Note that

For example:

[Root @ gd86 applications] # echo $ VaR
Hello: zkl: zyh: ABC
[Root @ gd86 applications] # expr substr $ var 1 2
He

2. String Truncation

Var = $ Var "$ Var" to complete string connection

3. traverse the substrings of strings separated by Spaces

For sub in $ strings

Do

Echo $ sub

Done

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.