Replace one substring for another string in shell script

前端 未结 10 1548
猫巷女王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:22

    For Dash all previous posts aren't working

    The POSIX sh compatible solution is:

    result=$(echo "$firstString" | sed "s/Suzi/$secondString/")
    

    This will replace the first occurrence on each line of input. Add a /g flag to replace all occurrences:

    result=$(echo "$firstString" | sed "s/Suzi/$secondString/g")
    

提交回复
热议问题