Variable in sed

Posted on 2004-11-29
Last Modified: 2008-01-02

I've the problem with my shell script:

sed 's/$search_word/$replace_word/g'  testfile
cat testfile

How do I make sed take variables $seach_word and $replace_word instead
of "SEARCH" and "REPLACE"?

Question by:wesly_chen
    LVL 38

    Author Comment

    It's bourne or bash shell.
    LVL 29

    Accepted Solution

    Try double quotes:

    sed "s/$search_word/$replace_word/g"  testfile
    LVL 38

    Author Comment

    You are right. I forget the difference between single quote and double quote.


    Expert Comment

    I have a question here.
    What if  the variable "replace_word" contains '/' . For ex: HOME variable in Linux

    sed "s/$search_word/$HOME/g"  testfile

    gives error "sed: -e expression #1, char 7: Unknown option to `s'"

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
    The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now