- 变量(字符串)变换
定义一个变量t,内容为framE [[email protected] tmp]# t=framE 查看变量t的内容:echo $t或者是echo ${t} [[email protected] tmp]# echo $t framE [[email protected] tmp]# 将变量t的首字母大写:echo ${t^} [[email protected] tmp]# echo ${t^} FramE [[email protected] tmp]# 将变量t的所有字母大写:echo ${t^^} [[email protected] tmp]# echo ${t^^} FRAME [[email protected] tmp]# 将变量t的首字母小写:echo ${t,} [[email protected] tmp]# echo ${t,} framE [[email protected] tmp]# 将变量t的所有字母小写:echo ${t,,} [[email protected] tmp]# echo ${t,,} frame [[email protected] tmp]# 将变量t的首字母大小写切换:echo ${t~} [[email protected] tmp]# echo ${t~} FramE [[email protected] tmp]# 将变量t的所有字母大小写切换:echo ${t~~} [[email protected] tmp]# echo ${t~~} FRAMe [[email protected] tmp]# 总结: ^:首字母大写 ^^:所有字母大写 ,:首字母小写 ,,:所有字母小写 ~:首字母大小写切换 ~~:所有字母大小写切换
- 移除匹配的字符串
定义一个变量filename,该变量的值为pwd所对应的当前路径 [[email protected] network-scripts]# filename="$(pwd)" [[email protected] network-scripts]# echo $filename /etc/sysconfig/network-scripts [[email protected] network-scripts]# 从前往后删,删除掉最短的一个"/" [[email protected] network-scripts]# echo ${filename#*/} etc/sysconfig/network-scripts [[email protected] network-scripts]# 从前往后删,删除掉最长的一个"/" [[email protected] network-scripts]# echo ${filename##*/} network-scripts [[email protected] network-scripts]# 从后往前删,删除掉最短的一个"/" [[email protected] network-scripts]# echo ${filename%/*} /etc/sysconfig [[email protected] network-scripts]# 从后往前删,删除掉最短的一个"/" [[email protected] network-scripts]# echo ${filename%%/*} [[email protected] network-scripts]# #:从前往后删,删除掉最短的一个 ##:从前往后删,删除掉最长的一个 %:从后往前删,删除掉最短的一个 %%:从后往前删,删除掉最长的一个
- 查找与替换
查看变量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]# 总结: /match/value:将第一次出现的match地换成value //match/value:将所有的match替换成value
- 其他字符串的操作符
查询字符串的长度:echo {#filename} [[email protected] network-scripts]# echo ${#filename} 30 [[email protected] network-scripts]# 字符串切片操作:${filename:offset:length} offset从0开始 [[email protected] network-scripts]# echo ${filename:5:9} sysconfig [[email protected] network-scripts]#
时间: 2025-01-04 08:16:07