更换一个子在shell脚本另一个字符串 [英] Replace one substring for another string in shell script

查看:145
本文介绍了更换一个子在shell脚本另一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我爱苏子和结婚,我想改变苏子,以萨拉。

 #!/斌/庆典
firstString =我爱苏子和结婚
secondString =萨拉
# 做一点事...

结果必须是这样的:

  firstString =我爱Sara和结婚


解决方案

您可以这样写:

  original_string ='我爱苏子嫁给
string_to_replace_Suzi_with =萨拉
result_string =$ {original_string /苏子/ $ string_to_replace_Suzi_with}

I have "I love Suzi and Marry" and I want to change "Suzi" to "Sara".

#!/bin/bash
firstString="I love Suzi and Marry"
secondString="Sara"
# do something...

The result must be like this:

firstString="I love Sara and Marry"

解决方案

You can write:

original_string='i love Suzi and Marry'
string_to_replace_Suzi_with=Sara
result_string="${original_string/Suzi/$string_to_replace_Suzi_with}"

这篇关于更换一个子在shell脚本另一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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