String manipulation in the shell

Source: Internet
Author: User

Recently summed up some of the shell's operations on strings, hoping to help you better understand the shell. Strings can also be manipulated in the shell through external commands such as awk, SED, but invoking these external commands is very different from the performance of the shell built-in operators, so it is recommended that you use built-in operators and functions to complete the operation of the string. Needless to say, let's take a look at the string operators and functions built into the shell.

1. Judge the value of reading the shell variable   

An expression Meaning Instance
${var} variable var value, same as $var

[Email protected]:~$ var= "test"

[Email protected]:~$ Echo ${var}

Test

${var-default}

[email protected]:~$ unset var

[email  protected]:~$ Echo ${var-default}

Default

[email protected]:~$ echo $var


[email  protected]:~$ var= ""

[email protected]:~$ Echo ${var-default}


[email  protected]:~$

${var:-default}

[email protected]:~$ unset var

[email protected]:~$ Echo ${var:-default}

Default

[email protected]:~$ Echo $var


[email protected]:~$ var= ""

[email protected]:~$ Echo ${ Var-default}

[email protected]:~$

${var=default} If Var is not declared, then default is used as the value of ${var-default}, and Var is assigned to default

[Email protected]:~$ unset var

[Email protected]:~$ Echo ${var=default}

Default

[Email protected]:~$ Echo $var

Default

[Email protected]:~$ var= "]

[Email protected]:~$ Echo ${var=default}


[Email protected]:~$

${var:=default} If Var is not declared, or its value is empty, then default is used as the value of ${var-default}, and Var is assigned to default

[Email protected]:~$ var= "]

[Email protected]:~$ Echo ${var:=default}

Default

[Email protected]:~$ Echo $var

Default

[Email protected]:~$ var= "]

[Email protected]:~$ Echo ${var=default}

Default

[Email protected]:~$

${var+default} If Var is declared, then its value is default, otherwise it is a null string

[Email protected]:~$ var=

[Email protected]:~$ Echo ${var+default}

Default

[Email protected]:~$ Echo ${var:+default}


[Email protected]:~$ Echo $var


[Email protected]:~$ var= "test"

[Email protected]:~$ Echo ${var+default}

Default

[Email protected]:~$ Echo ${var:+default}

Default

[Email protected]:~$ Echo $var

Test


${var:+default} If Var is set, then its value is default, otherwise it is a null string Ditto

${!test*}

${[email protected]}

Matches all previous variables declared with the start of test [Email protected]:~$ test1=

[Email protected]:~$ test2=

[Email protected]:~$ test3=

[Email protected]:~$ echo ${[email protected]}

Test Test1 test2 Test3

[Email protected]:~$ Echo ${!test*}

Test Test1 test2 Test3

[Email protected]:~$

2. String manipulation


An expression Defined Instance
${#var} Length of $var

[Email protected]:~$ var= "Hello"

[Email protected]:~$ echo ${#var}

5

[Email protected]:~$

${VAR:X} Extracting substrings starting from position X

[Email protected]:~$ Echo ${var:2}

Llo

${var:x:y} Extracts a substring of length y starting from position x

[Email protected]:~$ Echo ${var:2:2}

ll

${VAR#*STR} From the beginning of the variable $var, Delete the shortest matched substring of str

[Email protected]:~$ Echo ${var#*l}

Lo

${VAR##*STR} Delete the longest substring matching str from the beginning of the variable $var [Email protected]:~$ Echo ${var##*l}

O

${var%str*} Delete the shortest matched substring of str from the end of the variable $var

[Email protected]:~$ Echo ${var%l*}

Hel

${var%%str*} Delete the longest substring matching str from the end of the variable $var

[Email protected]:~$ Echo ${var%l*}

He

${VAR/SRC/DST} Use DST instead of the first one to match the SRC

[Email protected]:~$ Echo ${var/l/o}

Heolo

${VAR//SRC/DST} Use DST instead of all matching SRC

[Email protected]:~$ Echo ${var//l/o}

Heooo

${var/#src/DST} If the var prefix matches src, then DST is used instead of the matching SRC

[Email protected]:~$ echo ${var/#l/A}

Hello

[Email protected]:~$ echo ${var/#h/A}

Aell

${VAR/%SRC/DST} If the suffix of var matches src, then DST is used instead of the matching SRC

[Email protected]:~$ Echo ${var/%o/a}

Hella

PS: "str and SRC" can be regular expressions


This article is from the IT Worker blog, so be sure to keep this source http://sunhualong.blog.51cto.com/7175959/1760210

String manipulation in the shell

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.