Replace a string in shell script using a variable

后端 未结 10 1205
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 05:18

I am using the below code for replacing a string inside a shell script.

echo $LINE | sed -e \'s/12345678/\"$replace\"/g\'

but it\'s getting

10条回答
  •  星月不相逢
    2020-11-22 05:54

    Not specific to the question, but for folks who need the same kind of functionality expanded for clarity from previous answers:

    # create some variables
    str="someFileName.foo"
    find=".foo"
    replace=".bar"
    # notice the the str isn't prefixed with $
    #    this is just how this feature works :/
    result=${str//$find/$replace}
    echo $result    
    # result is: someFileName.bar
    
    str="someFileName.sally"
    find=".foo"
    replace=".bar"
    result=${str//$find/$replace}
    echo $result    
    # result is: someFileName.sally because ".foo" was not found
    

提交回复
热议问题