本文翻译自:Remove a fixed prefix/suffix from a string in Bash
In my bash
script I have a string and its prefix/suffix. 在我的bash
脚本中,我有一个字符串及其前缀/后缀。 I need to remove the prefix/suffix from the original string. 我需要从原始字符串中删除前缀/后缀。
For example, let's say I have the following values: 例如,假设我具有以下值:
string="hello-world"
prefix="hell"
suffix="ld"
How do I get to the following result? 如何获得以下结果?
result="o-wor"
#1楼
参考:https://stackoom.com/question/17kcN/从Bash中的字符串中删除固定的前缀-后缀
#2楼
$ foo=${string#"$prefix"}
$ foo=${foo%"$suffix"}
$ echo "${foo}"
o-wor
#3楼
Using sed: 使用sed:
$ echo "$string" | s