Chapter II ${of Shell string processing}

Source: Internet
Author: User

The previous section explains why referencing variables with ${}, ${} also has an important function, that is, text processing, a single line of text can basically meet all your needs.

3.1 Get string length

# var= ' Hello world! ' # echo $VARhello world!# echo ${#VAR}12

3.2 String slices

Intercept the Hello string: # var= ' Hello world! ' # echo ${var:0:5}hello Intercept World string: # echo ${var:5:-1}world Intercept last character: # echo ${var: (-1)}!

3.3 Replacement string

# var= ' Hello World world! ' Replace the first world string with world:# echo ${var/world/world}hello World world! Replace all world strings with world:# Echo ${var//world/world}hello World world!

3.4 String interception

# url= "to intercept the right string with a//delimiter: # echo ${url#*//} www.baidu.com/baike/user.html the right string with/for the delimiter: # echo ${url##*/}user.html to// The delimiter intercepts the left string: # Echo ${url%%//*} http: intercepts the left string with the/delimiter: # echo ${url%/*}http://www.baidu.com/baike to the left: # echo ${url%.* }http://www.baidu.com/baike/user to. The right side of the delimiter: # echo ${url##*.} Html

# Remove the left, match the first one from the left, # #从右边匹配第一个.

% remove right, match first from right, percent to match first from left.

This is the case with the * number.

3.5 Variable Status Assignment

${var:-string} returns a string if the VAR variable is empty

${var:+string} returns a string if the VAR variable is not empty

${var:=string} if the var variable is empty then re-assign the value of the var variable to string

${var:?string} outputs a string to stderr if the VAR variable is empty

Returns Hello world!:# var=# echo ${var:-' Hello world! '} If the variable is empty Hello world! If the variable is not empty return hello world!:# var= "Hello" [[email protected] ~]# echo ${var:+ ' Hello world! '} Hello world! If the variable is empty, re-assign the value: # var=# echo ${var:=hello}hello# echo $VARhello If the variable is empty the information output stderr:# var=# echo ${var:?value is Nu LL}-bash:var:value is null

${} The main purpose is probably so much, you can also get the array elements, in the following chapters.


Blog Address: http://lizhenliang.blog.51cto.com

QQ Group: Shell/python operation and maintenance development Group 323779636


Then we introduce the string output color, sometimes the key place needs to be bold, the color is the best way:

30: Black

31: Red

32: Green

33: Yellow

34: Blue

35: Purple

36: Dark green

37: white

40: Black

41: Crimson

42: Green

43: yellow

44: Blue

45: Purple

46: Dark green

47: White

0: terminal default settings

1: Highlight

4: underline

5: blinking

7: Anti-white display

8: hidden

Format:

\033[1;31;40m # 1 is the display mode, optional. 31 is the font color. 40m is the font background color.

\033[0M # Restores the terminal default color, that is, cancels the color setting.

Example:

#!/bin/bash# font color for i in {31..37}; Do echo-e "\033[$i; 40mHello world!\033[0m" done# background color for i in {41..47}; do echo-e "\033[47;${i}mhello world!\033[0m "done# display mode for i in {1..8}; Do echo-e "\033[$i, 31;40mhello world!\033[0m" done

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/8B/69/wKioL1hOEA3hOXTKAAA5ci8LYEk174.png-wh_500x0-wm_3 -wmp_4-s_1673390660.png "title=" Image.png "alt=" Wkiol1hoea3hoxtkaaa5ci8lyek174.png-wh_50 "/>

This article is from the "Li Zhenliang Technology Blog" blog, make sure to keep this source http://lizhenliang.blog.51cto.com/7876557/1881836

Chapter II ${of Shell string processing}

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.