《Linux运维总结:Shell脚本高级特性之字符串操作》
2026/9/12 17:14:58 网站建设 项目流程

总结:整理不易,如果对你有帮助,可否点赞关注一下?

更多详细内容请参考:Linux运维实战总结


一、字符串拼接

1、直接拼接(无空格)

root@k8s-master-58:~# a="hello"root@k8s-master-58:~# b="world"root@k8s-master-58:~# c=$a$broot@k8s-master-58:~# echo $chelloworld

2、使用双引号:双引号内的变量会被解析,所以可以在双引号内直接拼接字符串和变量。例如:

root@k8s-master-58:~# a="hello"root@k8s-master-58:~# b="world"root@k8s-master-58:~# echo "$a $b"hello world

3、使用反引号:反引号内的内容会被当作命令来执行,然后把输出的结果当作字符串来使用。例如:

root@k8s-master-58:~# a="hello"root@k8s-master-58:~# b="world"root@k8s-master-58:~# c=`echo "$a $b"`root@k8s-master-58:~# echo $chello world

4、混合变量与字面量。例如:

root@k8s-master-58:~# user="jack"root@k8s-master-58:~# greeting="Hello, $user! Today is $(date)"root@k8s-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便是截取的字符的个数

root@k8s-master-58:~# url=https://www.baidu.com/index.html/abc.com/map.jsroot@k8s-master-58:~# echo ${url: 0 :5}https

示例二:从左边第2个字符开始,5便是截取的字符的个数

root@k8s-master-58:~# url=https://www.baidu.com/index.html/abc.com/map.jsroot@k8s-master-58:~# echo ${url: 1 :5}ttps:

示例三:从左边第9个字符开始,截取之后所有内容,直至结束

root@k8s-master-58:~# url=https://www.baidu.com/index.html/abc.com/map.jsroot@k8s-master-58:~# echo ${url: 8}www.baidu.com/index.html/abc.com/map.js

2.1.2、从字符串右边开始计数

说明:从字符串的右边开始计数,那么截取字符串的具体格式如下:${string: 0-start :length},位置参数是0-start ,这是固定写法。length是要截取的长度,省略的话表示直到字符串的末尾。

示例一:从右边第七个字符开始,向后(右)截取5个字符内容

root@k8s-master-58:~# url=https://www.baidu.com/index.html/abc.com/map.jsroot@k8s-master-58:~# echo ${url: 0-7 :5}/map.

示例二:从右边第七个字符开始,向后(右)截取所有的内容

root@k8s-master-58:~# url=https://www.baidu.com/index.html/abc.com/map.jsroot@k8s-master-58:~# echo ${url: 0-7}/map.js

2.2、从指定子字符串开始截取

2.2.1、#和##号截取字符串(删左边留右边)

说明:${var#*string}:删除第一次出现的 string 及其左侧全部内容。

root@k8s-master-58:~# url=https://www.baidu.com/index.htmlroot@k8s-master-58:~# echo ${url#*com}/index.html

说明:${var##*string}:删除最后一次出现的 string 及其左侧全部内容。

root@k8s-master-58:~# url=https://www.baidu.com/index.html/abc.com/map.jsroot@k8s-master-58:~# echo ${url##*com}/map.js

2.2.2、%和%%号截取(删右边留左边)

说明:${var%string*}:删掉变量值中最后一次出现的string,以及它后面的所有字符。

root@k8s-master-58:~# url=https://www.baidu.com/index.html/abc.com/map.jsroot@k8s-master-58:~# echo ${url%com*}https://www.baidu.com/index.html/abc.

说明:${var%%string*}:删掉变量值中第一次出现的 string,以及它后面的所有字符。

root@k8s-master-58:~# url=https://www.baidu.com/index.html/abc.com/map.jsroot@k8s-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}

root@k8s-master-58:~# url=https://www.baidu.com/index.html/abc.com/map.jsroot@k8s-master-58:~# echo ${url/www.baidu.com/www.sz.com}https://www.sz.com/index.html/abc.com/map.js

示例二:${var//char1/char2}

root@k8s-master-58:~# url=https://www.baidu.com/index.html/abc.com/map.jsroot@k8s-master-58:~# echo ${url//com/cn}https://www.baidu.cn/index.html/abc.cn/map.js

示例三:${var/#char1/char2}

root@k8s-master-58:~# url=https://www.baidu.com/index.html/abc.com/map.jsroot@k8s-master-58:~# echo ${url/#https/http}http://www.baidu.com/index.html/abc.com/map.js

示例四:${var/%char1/char2}

root@k8s-master-58:~# url=https://www.baidu.com/index.html/abc.com/map.jsroot@k8s-master-58:~# echo ${url/%js/html}https://www.baidu.com/index.html/abc.com/map.html

三、子字符串删除

示例一:仅删除第一次出现的子字符串:${var/char}

root@k8s-master-58:~# url=https://www.baidu.com/index.html/abc.com/map.htmlroot@k8s-master-58:~# echo ${url/com}https://www.baidu./index.html/abc.com/map.html

示例二:删除所有出现的内容:${var//char}

root@k8s-master-58:~# url=https://www.baidu.com/index.html/abc.com/map.htmlroot@k8s-master-58:~# echo ${url//com}https://www.baidu./index.html/abc./map.html

总结:整理不易,如果对你有帮助,可否点赞关注一下?

更多详细内容请参考:Linux运维实战总结

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询