String operations in bash Script Programming

Source: Internet
Author: User

One of the string operations: test the existence of Variables

$ {Varname:-word} If varname exists and is not null, return its value; otherwise, return word
$ {Varname: = word} If varname exists and is not null, return its value. Otherwise, set it to word and return its value.
$ {Varname :? Message} If varname exists and is not null, return its value. Otherwise, print varname: followed by message and exit the current command or script.
$ {Varname: + word} If varname exists and is not null, word is returned; otherwise, null is returned.
$ {Varname: Offset} Offset characters, take all remaining characters
$ {Varname: offset: length} Offset characters, length characters
# Echo $ {age:-30} // If the variable Age is not set, 3030 # age = 28 # echo $ {age:-30} is returned. // If the variable age is set to 28, 2828 # name = King # echo $ {Name: = Tom} is returned. // If the variable name is set to King, kingking # unset name # echo $ {Name: = Tom} // The variable name is cleared. If not set, set the variable name to tomtom # echo $ nametom # echo $ {sex :? "What's your sex? "}-Bash: Sex: What's your sex? // The variable sex is not set to show what's your sex? # Sex = female # echo $ {sex :? "What's your sex? "} // The variable sex has been set. femalefemale # echo $ {ID: +" not null "} // The variable ID is not set, returns NULL # id = 100 # echo $ {ID: + "not null"} // The variable ID has been set. The returned message is not nullnot null # echo $ {MSG: 5} // offset of 5 characters, take all remaining characters world # echo $ {MSG: 5: 2} // offset of 5 characters, take two characters wo


String operation 2: calculate the number of characters in the Variable

$ {# Varname}

# echo $msgHelloWorld# echo ${#msg} 10


String operation 3: Use mode and mode matching to operate the string

$ {Varname # pattern}
If the pattern matches the beginning of the variable value, delete the shortest matching part and return the remaining part.
$ {Varname ## pattern} If the pattern matches the beginning of the variable value, delete the longest matching part and return the remaining part.
$ {Varname % pattern} If the pattern matches the end of the variable value, delete the shortest matching part and return the remaining part.
$ {Varname % pattern} If the pattern matches the end of the variable value, delete the longest matching part and return the remaining part.
$ {Varname/pattern/string} Replace the first part that matches pattern with string
$ {Varname // pattern/string} Replace all matching parts of pattern with string
# Var=/etc/sysconfig/network-scripts/ifcfg-eth0      # echo $Var/etc/sysconfig/network-scripts/ifcfg-eth0# echo ${Var#/*/}sysconfig/network-scripts/ifcfg-eth0# echo ${Var##/*/}ifcfg-eth0# Var=/home/yy/test.conf.bak# echo $Var/home/yy/test.conf.bak# echo ${Var%.*}/home/yy/test.conf# echo ${Var%%.*}/home/yy/test# echo $msgHelloWorld# echo ${msg/o/O}  HellOWorld# echo ${msg//o/O} HellOWOrld


This article from the "Tiger, tiger, shengwei" blog, please be sure to keep this source http://tobeone.blog.51cto.com/817917/1557428

String operations in bash Script Programming

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.