Replace one substring for another string in shell script

前端 未结 10 1591
猫巷女王i
猫巷女王i 2020-11-22 11:55

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 some         


        
10条回答
  •  借酒劲吻你
    2020-11-22 12:16

    This can be done entirely with bash string manipulation:

    first="I love Suzy and Mary"
    second="Sara"
    first=${first/Suzy/$second}
    

    That will replace only the first occurrence; to replace them all, double the first slash:

    first="Suzy, Suzy, Suzy"
    second="Sara"
    first=${first//Suzy/$second}
    # first is now "Sara, Sara, Sara"
    

提交回复
热议问题