Linux Shell 截取字符串
2023-09-11 14:18:54 时间
Linux Shell 截取字符串
shell中截取字符串的方法很多
${var#*/} ${var##*/} ${var%/*} ${var%%/*} ${var:start:len} ${var:start} ${var:0-start:len} ${var:0-start}
下面用几个例子展示一下:
1) 获得字符串的长度
语法:
${#var}
示例代码:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string" echo "string : [${str}]" length=${#str} echo "length : [${length}]"
执行结果:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string] length : [61]
2) 使用 # 和 ## 获取尾部子字符串
2.1) # 最小限度从前面截取word
语法:
${parameter#word}
示例代码:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string" echo "string : [${str}]" #分割符为'/' substr=${str#*/} echo "substr : [${substr}]"
执行结果:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string] substr : [/www.fengbohello.xin3e.com/blog/shell-truncating-string]
2.2) ## 最大限度从前面截取word
语法:
${parameter##word}
示例代码:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string" echo "string : [${str}]" #分割符为'/' substr=${str##*/} echo "substr : [${substr}]"
执行结果:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string] substr : [shell-truncating-string]
3) 使用 % 和 %% 获取头部子字符串
3.1) % 最小限度从后面截取word
语法:
${parameter%word}
示例代码:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string" echo "string : [${str}]" substr=${str%/*} echo "substr : [${substr}]"
执行结果:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string] substr : [http://www.fengbohello.xin3e.com/blog]
3.2) %% 最大限度从后面截取word
语法:
${parameter%%word}
示例代码:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string" echo "string : [${str}]" substr=${str%%/*} echo "substr : [${substr}]"
执行结果:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string] substr : [http:]
4)使用 ${var:} 模式获取子字符串
4.1) 指定从左边第几个字符开始以及子串中字符的个数
语法:
${var:start:len}
示例代码:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string" echo "string : [${str}]" #其中的 0 表示左边第一个字符开始,7 表示子字符的总个数。 substr=${str:0:7} echo "substr : [${substr}]"
执行结果:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string] substr : [http://]
4.2) 从左边第几个字符开始一直到结束
语法:
${var:7}
示例代码:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string" echo "string : [${str}]" #其中的 7 表示左边第8个字符开始 substr=${str:7} echo "substr : [${substr}]"
执行结果:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string] substr : [www.fengbohello.xin3e.com/blog/shell-truncating-string]
4.3) 从右边第几个字符开始以及字符的个数
语法:
${var:0-start:len}
示例代码:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string" echo "string : [${str}]" #其中的 0-23 表示右边算起第23个字符开始,5 表示字符的个数 substr=${str:0-23:5} echo "substr : [${substr}]"
执行结果:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string] substr : [shell]
4.4) 从右边第几个字符开始一直到结束
语法:
${var:0-start}
示例代码:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string" echo "string : [${str}]" #其中的 0-6 表示右边算起第6个字符开始 substr=${str:0-6} echo "substr : [${substr}]"
执行结果:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string] substr : [string]
相关文章
- linux,shell脚本中获取脚本的名字,使用脚本的名字。
- Linux Command cut 切割
- 跟老男孩学Linux运维:Shell编程实战3.2 环境变量
- linux学习-Linux系统启动过程
- Linux修改Shell命令提示符及颜色
- Linux Deploy在安卓手机安装LINUX系统
- Linux学习---linux下的彩蛋和各种有趣的命令
- Linux下Qt打包的shell脚本
- Linux内核project导论——linux学习和职业曲线(刚開始学习的人,中级,高级都可參考)
- Linux - Bash shell的功能;内建命令type
- Linux 之 arm linux 板子与 ubuntu 使用 nfs 进行 mount 挂载进行文件传输的简单整理
- 【Linux shell】grep命令精确匹配字符串查找
- Linux Shell的 & 、&& 、 ||
- 《Linux命令行与shell脚本编程大全 第3版》高级Shell脚本编程---26
- 《Linux命令行与shell脚本编程大全 第3版》高级Shell脚本编程---24
- 《Linux命令行与shell脚本编程大全 第3版》高级Shell脚本编程---04
- 《Linux命令行与shell脚本编程大全 第3版》Shell脚本编程基础---57
- 《Linux命令行与shell脚本编程大全 第3版》Shell脚本编程基础---36
- 《Linux命令行与shell脚本编程大全 第3版》Shell脚本编程基础---34
- 《Linux命令行与shell脚本编程大全 第3版》Shell脚本编程基础---24
- 《Linux命令行与shell脚本编程大全 第3版》Shell脚本编程基础---23
- 《Linux命令行与shell脚本编程大全 第3版》Shell脚本编程基础---06
- 《Linux命令行与shell脚本编程大全 第3版》Shell脚本编程基础---03
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---47