庆典 - 从字符串中删除一个固定的preFIX /后缀 [英] bash - remove a fixed prefix/suffix from a string

查看:144
本文介绍了庆典 - 从字符串中删除一个固定的preFIX /后缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的庆典脚本我有一个字符串和preFIX /后缀。我需要从原始字符串中删除preFIX /后缀。

例如,假设我有以下值:

 字符串=你好世界
preFIX =地狱
后缀=LD

我如何得到下面的结果呢?

 结果=邻窝


解决方案

  $富= $ {#字符串$$ p $} PFIX
$ foo的= $ {富%$后缀}
$回声$ {}富
邻WOR

In my bash script I have a string and its prefix/suffix. 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"

解决方案

$ foo=${string#$prefix}
$ foo=${foo%$suffix}
$ echo "${foo}"
o-wor

这篇关于庆典 - 从字符串中删除一个固定的preFIX /后缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆