资讯动态

《Linux运维总结:Shell脚本高级特性之字符串操作》

发布时间:2026/9/12 17:15:05 来源:尧图企业网站定制
总结整理不易如果对你有帮助可否点赞关注一下更多详细内容请参考Linux运维实战总结一、字符串拼接1、直接拼接无空格rootk8s-master-58:~# ahellorootk8s-master-58:~# bworldrootk8s-master-58:~# c$a$brootk8s-master-58:~# echo $chelloworld2、使用双引号双引号内的变量会被解析所以可以在双引号内直接拼接字符串和变量。例如rootk8s-master-58:~# ahellorootk8s-master-58:~# bworldrootk8s-master-58:~# echo $a $bhello world3、使用反引号反引号内的内容会被当作命令来执行然后把输出的结果当作字符串来使用。例如rootk8s-master-58:~# ahellorootk8s-master-58:~# bworldrootk8s-master-58:~# cecho $a $brootk8s-master-58:~# echo $chello world4、混合变量与字面量。例如rootk8s-master-58:~# userjackrootk8s-master-58:~# greetingHello, $user! Today is $(date)rootk8s-master-58:~# echo $greetingHello, jack!Today is Thu Aug1409:49:32 AM CST2025二、子字符串截取格式说明${string: start :length}从 string 字符串的左边第 start 个字符开始向右截取 length 个字符。${string: start}从 string 字符串的左边第 start 个字符开始截取直到最后。${string: 0-start :length}从 string 字符串的右边第 start 个字符开始向右截取 length 个字符。${string: 0-start}从 string 字符串的右边第 start 个字符开始截取直到最后。${string#*chars}从左边开始删除第一个chars以及左边的所有字符保留右边字符。${string##*chars}从左边开始删除最后一个chars以及左边的所有内容保留右边字符${string%chars*}从右边开始删除遇到的第一个chars以及右边所有的内容保留左边字符${string%%chars*}从右边开始删除遇到的最后也就是最左边一个chars以及右边所有内容保留左边字符2.1、从指定位置开始截取说明从指定位置截取截掉左边保留右边。2.1.1、从字符串左边开始计数说明{string: start: length},string 是要截取的字符串start 是起始位置省略的话表示从0开始length是要截取的长度省略的话表示直到字符串的末尾。示例一从左边第0个字符开始5便是截取的字符的个数rootk8s-master-58:~# urlhttps://www.baidu.com/index.html/abc.com/map.jsrootk8s-master-58:~# echo ${url: 0 :5}https示例二从左边第2个字符开始5便是截取的字符的个数rootk8s-master-58:~# urlhttps://www.baidu.com/index.html/abc.com/map.jsrootk8s-master-58:~# echo ${url: 1 :5}ttps:示例三从左边第9个字符开始截取之后所有内容直至结束rootk8s-master-58:~# urlhttps://www.baidu.com/index.html/abc.com/map.jsrootk8s-master-58:~# echo ${url: 8}www.baidu.com/index.html/abc.com/map.js2.1.2、从字符串右边开始计数说明从字符串的右边开始计数那么截取字符串的具体格式如下${string: 0-start :length}位置参数是0-start ,这是固定写法。length是要截取的长度省略的话表示直到字符串的末尾。示例一从右边第七个字符开始向后右截取5个字符内容rootk8s-master-58:~# urlhttps://www.baidu.com/index.html/abc.com/map.jsrootk8s-master-58:~# echo ${url: 0-7 :5}/map.示例二从右边第七个字符开始向后右截取所有的内容rootk8s-master-58:~# urlhttps://www.baidu.com/index.html/abc.com/map.jsrootk8s-master-58:~# echo ${url: 0-7}/map.js2.2、从指定子字符串开始截取2.2.1、#和##号截取字符串删左边留右边说明${var#*string}删除第一次出现的 string 及其左侧全部内容。rootk8s-master-58:~# urlhttps://www.baidu.com/index.htmlrootk8s-master-58:~# echo ${url#*com}/index.html说明${var##*string}删除最后一次出现的 string 及其左侧全部内容。rootk8s-master-58:~# urlhttps://www.baidu.com/index.html/abc.com/map.jsrootk8s-master-58:~# echo ${url##*com}/map.js2.2.2、%和%%号截取删右边留左边说明${var%string*}删掉变量值中最后一次出现的string以及它后面的所有字符。rootk8s-master-58:~# urlhttps://www.baidu.com/index.html/abc.com/map.jsrootk8s-master-58:~# echo ${url%com*}https://www.baidu.com/index.html/abc.说明${var%%string*}删掉变量值中第一次出现的 string以及它后面的所有字符。rootk8s-master-58:~# urlhttps://www.baidu.com/index.html/abc.com/map.jsrootk8s-master-58:~# echo ${url%%com*}https://www.baidu.三、子字符串替换格式说明${var/char1/char2}使用char2, 来代替第一个匹配的char1${var//char1/char2}使用char2,代替所有匹配的char1${var/#char1/char2}如果var的前缀匹配char1, 那么就用char2来代替匹配到的$char1${var/%char1/char2}如果var的后缀匹配char1, 那么就用$char2来代替匹配到的char1示例一${var/char1/char2}rootk8s-master-58:~# urlhttps://www.baidu.com/index.html/abc.com/map.jsrootk8s-master-58:~# echo ${url/www.baidu.com/www.sz.com}https://www.sz.com/index.html/abc.com/map.js示例二${var//char1/char2}rootk8s-master-58:~# urlhttps://www.baidu.com/index.html/abc.com/map.jsrootk8s-master-58:~# echo ${url//com/cn}https://www.baidu.cn/index.html/abc.cn/map.js示例三${var/#char1/char2}rootk8s-master-58:~# urlhttps://www.baidu.com/index.html/abc.com/map.jsrootk8s-master-58:~# echo ${url/#https/http}http://www.baidu.com/index.html/abc.com/map.js示例四${var/%char1/char2}rootk8s-master-58:~# urlhttps://www.baidu.com/index.html/abc.com/map.jsrootk8s-master-58:~# echo ${url/%js/html}https://www.baidu.com/index.html/abc.com/map.html三、子字符串删除示例一仅删除第一次出现的子字符串${var/char}rootk8s-master-58:~# urlhttps://www.baidu.com/index.html/abc.com/map.htmlrootk8s-master-58:~# echo ${url/com}https://www.baidu./index.html/abc.com/map.html示例二删除所有出现的内容${var//char}rootk8s-master-58:~# urlhttps://www.baidu.com/index.html/abc.com/map.htmlrootk8s-master-58:~# echo ${url//com}https://www.baidu./index.html/abc./map.html总结整理不易如果对你有帮助可否点赞关注一下更多详细内容请参考Linux运维实战总结

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价