Bash Series (4)--bash variable operation

Source: Internet
Author: User

    • Variable (String) transformation


Define a variable T with the content Frame[[email protected] tmp]# t=frame view the contents of the variable T:echo  $t or Echo ${t}[[email  protected] tmp]# echo  $tframE [[email protected] tmp] #将变量t的首字母大写: echo ${t^} [[email protected] tmp]# echo ${t^}frame[[email protected] tmp]#  Capitalize all the letters of the variable T:echo ${t^^}[[email protected] tmp]# echo ${t^^}frame[[email  protected] tmp]#  the first letter of the variable T lowercase: echo ${t,}[[email protected] tmp]# echo ${t,} FRAME[[EMAIL PROTECTED] TMP] #将变量t的所有字母小写:echo ${t,,}[[email protected] tmp]#  echo ${t,,}frame[[email protected] tmp]#  Switch The first letter of the variable T to the case:echo ${t~}[[email  PROTECTED] TMP]# ECHO ${T~}FRAME[[EMAIL PROTECTED] TMP] #将变量t的所有字母大小写切换: echo ${t~ ~}[[email protected] tmp]# echo ${t~~}frame[[email protected] tmp]#  Summary: ^ : First Capital ^ ^: all uppercase letters,: lowercase,,: All Letters lowercase ~: First letter Case Toggle ~ ~:Have letter case Toggle 




    • Remove a matching string

Defines a variable filename, the value of which is the current path corresponding to PWD [[Email protected] network-scripts]# filename= $ (PWD)] [[Email  protected] network-scripts]# echo  $filename/etc/sysconfig/network-scripts[[email  protected] network-scripts]#  Delete the shortest "/" [[email protected] network-scripts]#  from the trip echo ${filename#*/}etc/sysconfig/network-scripts[[email protected] network-scripts]#  from the post to delete , remove the longest "/" [[email protected] network-scripts]# echo ${filename##*/}network-scripts[[ email protected] network-scripts]#  from the back, delete the shortest one "/" [[email protected]  network-scripts]# echo ${filename%/*}/etc/sysconfig[[email protected] network-scripts]#   Delete from the back, remove the shortest "/" [[Email protected] network-scripts]# echo ${filename%%/*}[[email  protected] network-scripts]# #: Deleted from the past, delete the shortest one # #: The previous deletion, delete the longest one%: from the back to delete, remove the shortest one percent: Remove the longest one



    • Find and replace


View the contents of the variable filename: [[email protected] network-scripts]# echo $filename/etc/sysconfig/network-scripts[[email protected] Network-scripts] #将第一次出现的小写s替换成大写的S [[email protected] network-scripts]# echo ${filename/s/s}/etc/sysconfig/ Network-scripts[[email protected] network-scripts] #将所有的小写s替换成大写的S [[email protected] network-scripts]# echo ${ Filename//s/s}/etc/sysconfig/network-scripts[[email protected] network-scripts]# Summary:/match/ Value: Replace the first occurrence of match with Value//match/value: Replaces all match with value



    • Operators for other strings

Length of query string: Echo {#filename}[[email protected] network-scripts]# echo ${#filename}30[[email protected] network-scripts]# String slicing operation: ${filename:offset:length} offset starting from 0 [[email protected] network-scripts]# echo ${filename:5:9}sysconfig[[ Email protected] network-scripts]#





This article is from the "Felix Zhang" blog, please be sure to keep this source http://hezhanglinux.blog.51cto.com/10861477/1711388

Bash Series (4)--bash variable operation

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.