BASH Shell (string) Operation Summary

Source: Internet
Author: User

Basically, the articles referenced below add a few items to make a record, so you are too lazy to remember them, saving you the trouble of Google every time.

Http://my.oschina.net/aiguozhe/blog/41557

1. Get the length

1

STR = "ABCD"

2

Expr length $ STR #4

3

Echo $ {# STR} #4

4

Expr "$ Str ":
". *" #4

It seems that the second type is generally used.

2. Locate the substring

1

STR = "ABC"

2

Expr index $ Str ""
#1

3

Expr index $ Str "B"
#2

4

Expr index $ Str "X"
#0

5

Expr index $ Str ""
#0

3. Select substrings

1

STR = "abcdef"

2

Expr substr "$ Str" 1 3
# Take 3 characters from the first position, ABC

3

Expr substr "$ Str" 2 5
# Start from the second position and take 5 characters, bcdef

4

Expr substr "$ Str" 4 5
# Start from the fourth position with 5 characters, def

5

 

6

Echo $ {STR: 2}
# Extract the string from the second position, bcdef

7

Echo $ {STR: 2: 3}
# Extract 3 characters from the second position, BCD

8

Echo $ {STR :(-2 )}
# Extract the string from the second position to the left, ABCDE

9

Echo $ {STR :(-2): 3}
# Extract 6 characters from the penultimate position to the left, CDE

 

4. Capture substrings

01

STR = "abbc, def, Ghi, abcjkl"

02

Echo $ {STR # A * c} #, def, Ghi, abcjkl
A well number (#) indicates the shortest matching from the left

03

Echo $ {STR # A * c} # jkl,
The two pound signs (#) represent the longest match on the left.

04

Echo $ {STR # "A * C "}#
Null, because STR does not contain the substring "A * C"

05

Echo $ [STR ## "A * C "}#
Null. Likewise

06

Echo $ {STR # D * f) # abbc, def, Ghi, abcjkl,

07

Echo $ {STR # * D * f} #, Ghi, abcjkl

08

 

09

Echo $ {STR % A * l}
# A percent sign (%) for abbc, def, and Ghi indicates the shortest match is intercepted from the right side.

10

Echo $ {STR % B * l}
# A two percentage signs (%) indicates the longest matching from the right side

11

Echo $ {STR % A * c}
# Abbc, def, Ghi, abcjkl

It can be remembered that the pound sign (#) is usually used to represent a number, which is placed in front; The percent sign (%) is unmounted to the back of the number;
Or in this way, in the keyboard layout, the well number (#) is always on the left of the percent sign (%) (that is, the Front ):-)

5. String replacement

1

STR = "Apple, tree, apple tree"

2

Echo $ {str/Apple}
# Replace the first Apple

3

Echo $ {STR // Apple/Apple}
# Replace all apple

4

 

5

Echo $ {str/# Apple/Apple }#
If the STR string starts with Apple, replace it with Apple.

6

Echo $ {str/% Apple/Apple}
# If the string STR ends with Apple, replace it with apple

 

6. Comparison

* A single group [] is acceptable.

1

[["A.txt" = A *]
# Pattern matching)

2

[["A.txt" = ~ . * \. Txt]
# RegEx matching)

3

[["ABC" =
"ABC"] #
Logical truth (string comparision)

4

[["11" <
"2"] #
Logical truth (string comparision), compared by ASCII Value

 

7. Connection

1

S1 = "hello"

2

S2 = "world"

3

Echo ${S1 }$ {S2 }#
Of course, you can write $ S1 $ S2 in this way, but you 'd better add braces.

 

8. Save the command execution result in the variable.

1

Platform = 'uname-p'

2

Currentpath = 'pwd'

* Note that the two sides are not single quotes.

9. Split the string into an array.

1

Arguments = ($ (echo $ {x} | tr
"," "\ N "))# X is the input parameter, separated by commas

10. case-insensitive Conversion

Debpackname ='Echo $2 | awk '{print tolower ($0 )}''
# Convert to lowercase

Debpackname ='Echo $2 | awk '{print toupper ($0 )}'`
# Convert to uppercase

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.