Shell字符串种定位字符串位置

本文遵循BY-SA版权协议,转载请附上原文出处链接。


本文作者: 黑伴白

本文链接: http://heibanbai.com.cn/posts/96781f81/

Shell字符串定位字符串位置

在写Shell脚本时,我们经常会用到在字符串1种查找字符串2的位置,本文介绍几种方式

方法1

1
2
3
4
5
6
7
8
a="after rain the sky looks blue"
b="sky"
strindex() {
x="${1%%$2*}"
[[ $x = $1 ]] && echo -1 || echo ${#x}
}
strindex "$a" "$b" # prints 15
strindex "$a" "test" # prints -1

方法2


蚂蚁再小也是肉🥩!


Shell字符串种定位字符串位置
http://heibanbai.com.cn/posts/96781f81/
作者
黑伴白
发布于
2022年10月10日
许可协议

“您的支持,我的动力!觉得不错的话,给点打赏吧 ୧(๑•̀⌄•́๑)૭”

微信二维码

微信支付

支付宝二维码

支付宝支付