网站首页 > 基础教程 正文
在编写Shell脚本时,字符串操作不可避免会遇到。本文汇总Shell编写中常用的字符串操作,以方便大家使用。
Shell中字符串操作主要有两种方式:参数替换、expr命令。
计算字符串长度
参数替换法,${#string}
示例,
aneirin@host-1:~$ mystring=abcdefg12345
aneirin@host-1:~$ echo ${#mystring}
12
expr命令,expr length $string
示例,
aneirin@host-1:~$ expr length $mystring
12
字符串抽取
${string:position}
从$string的位置$position抽取子串,如果$string是“*”或者“@”,则从位置参数抽取。
${string:position:length},通过“$length”控制抽取子串的长度
示例,
aneirin@host-1:~$ echo ${mystring:3}
defg12345
aneirin@host-1:~$ echo ${mystring:3:3}
def
参数position可以为负值,意思是从字符串右边操作
aneirin@host-1:~$ echo ${mystring:(-3)} //括号不能少。如果不写括号,需要空格
345
使用expr命令也是可以的,
expr substr $string $position $length,注意一点,expr方法索引从1开始,参数替换方法索引从0开始
子串删除
${string#substring},从$string开头删掉最短匹配的子串$substring
${string##substring},从$string开头删掉最长匹配的子串$substring
${string%substring},从$string结尾删掉最短匹配的子串$substring
${string%%substring},从$string结尾删掉最长匹配的子串$substring
字符串替换
${
string/substring/replacement},使用$replacement替换第一个匹配的子串$substring
${
string//substring/replacement},使用$replacement替换所有匹配的子串$substring
aneirin@host-1:~$ mystring=abc123abc456
aneirin@host-1:~$ echo ${mystring//abc/ABC}
ABC123ABC456
还有比较特殊的字符串替换,
${string/#substring/replacement},如果$substring匹配$string的开头,则用$replacement替换$substring
${
string/%substring/replacement},如果$substring匹配$string的结尾,则用$replacement替换$substring
aneirin@host-1:~$ mystring=abc123abc
aneirin@host-1:~$ echo ${mystring/#abc/XYZ}
XYZ123abc
aneirin@host-1:~$ echo ${mystring/%abc/XYZ}
abc123X
给定一个正则表达式,求其匹配字符串的长度
expr match "$string" '$substring',其中“$substring”是一个正则表达式
aneirin@host-1:~$ mystring=abc123abca
aneirin@host-1:~$ echo $(expr match "$mystring" 'abc[0-9]*')
6
上面示例匹配的字符串是“abc123”,长度刚好是6。下面写法也可以,
expr "$mystring" : 'abc[0-9]*',冒号两边要有空格,否则报语法错误。
计算子串在字符串中的索引
expr index $string $substring,返回匹配的$substring的第一个字母在$string中的索引
aneirin@host-1:~$ mystring=abc123abca
aneirin@host-1:~$ expr index $mystring cwfe
3
总结
Shell中字符串的操作经常会遇到,文章对字符串的常用操作做了简单介绍,希望能帮到正在努力的你,欢迎点赞评论!
猜你喜欢
- 2025-03-26 linux之shell函数详解(linux shell语句)
- 2025-03-26 shell常用命令之awk用法介绍(shell awk gsub)
- 2025-03-26 shell里面的一些特殊符号(shell变量中含有特殊字符)
- 2025-03-26 Shell脚本怎么写?Linux命令之awk上期(awk是一门语言概述)
- 2025-03-26 每天三分钟搞定linux shell脚本16 shell命令行中的特殊参数
- 2025-03-26 Linux,shell,数组,declare关联数组,二维数组,代码案例
- 2025-03-26 shell编程(shell编程百度网盘)
- 2025-03-26 100个经典实用的shell脚本,可自由复制,拿来即用建议收藏
- 2025-03-26 Linux 使用shell进行逐行文本求和
- 2025-03-26 shell里那些让人眼花缭乱的括号(shell小括号和大括号)
- 06-18单例模式谁都会,破坏单例模式听说过吗?
- 06-18Objective-c单例模式的正确写法「藏」
- 06-18单例模式介绍(单例模式都有哪些)
- 06-18前端设计-单例模式在实战中的应用技巧
- 06-18PHP之单例模式(php单例模式连接数据库)
- 06-18设计模式:单例模式及C及C++实现示例
- 06-18python的单例模式(单例 python)
- 06-18你认为最简单的单例模式,东西还挺多
- 最近发表
- 标签列表
-
- jsp (69)
- gitpush (78)
- gitreset (66)
- python字典 (67)
- dockercp (63)
- gitclone命令 (63)
- dockersave (62)
- linux命令大全 (65)
- pythonif (86)
- location.href (69)
- dockerexec (65)
- tail-f (79)
- queryselectorall (63)
- location.search (79)
- bootstrap教程 (74)
- 单例 (62)
- linuxgzip (68)
- 字符串连接 (73)
- html标签 (69)
- c++初始化列表 (64)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)