本文首发于我的Github博客
本文记录了作者使用bash过程中,为了解决去除前缀后缀的问题而了解到的bash的字符串操作,简单来说
string:position
或者string:position:length
进行子串提取string#pattern
进行最短匹配前缀去除,string##pattern
进行最长匹配前缀去除string%pattern
进行最短匹配后缀去除,string%%pattern
进行最长匹配后缀去除string/pattern/string
进行首个pattern
替换,string//pattern/string
进行全部pattern
替换- 以上的
pattern
指的都是wildcard而不是regular expression
场景
有时在使用bash脚本的时候,希望对字符串做一些基本的操作,比如说去除前缀后缀(去除文件扩展名,统一前缀编号等),事实上,bash原生支持了许多字符串操作
bash 中的字符串操作
下面所有的pattern
指的都是b