Replace a text with a variable

68,139

You need to use double quotes:

$ sed -i "s/wiki_host/${host_name}/g" /root/bin/sync

Your single quotes prevent the shell variable from being replaced with its contents.

Share:
68,139
Vince
Author by

Vince

Updated on April 20, 2020

Comments

  • Vince
    Vince about 4 years

    How can I do this?

    sed -i 's/wiki_host/$host_name/g' /root/bin/sync
    

    It will replace wiki_host with the text $host_name. But I want to replace it with the content of the variable..

    I tried it with

    sed -i 's/wiki_host/${host_name}/g' /root/bin/sync
    

    It doesn't work either.

  • wisbucky
    wisbucky about 6 years
    One important caveat is that the sed delimiter (/ in this case) cannot be part of ${host_name}, otherwise you will get errors.
  • Kristjan Adojaan
    Kristjan Adojaan about 4 years
    In the last case you have to use other delimiters for sed, then it works with replacement value containing slashes as well. For example $ sed -i "s#wiki_host#${host_name}#g" /root/bin/sync