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
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
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
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