Results 1 to 5 of 5

Thread: bash variables within sed commands not working!

  1. #1
    Join Date
    Mar 2005
    Beans
    51

    bash variables within sed commands not working!

    I'm trying to do a replacement with sed inside a bash script. My sed command looks something like the following

    sed "s/TEXT/$VAR/" input_file

    But sed keeps complaining at me about the $ character. Is it possible to put a variable inside sed in this manner?

  2. #2
    Join Date
    Jul 2005
    Beans
    152
    Distro
    Kubuntu 7.10 Gutsy Gibbon

    Re: bash variables within sed commands not working!

    Code:
    $ cat >> test << EOF
    this is a test
    Ctrl-D
    $ VAR=test
    $ sed s/$VAR/toast/ test
    this is a toast
    Works with $VAR in replacement or searching position. What are you trying to expand $VAR to? What happens when you do the small example above?

  3. #3
    Join Date
    Sep 2006
    Beans
    2,914

    Re: bash variables within sed commands not working!

    show your code

  4. #4
    Join Date
    Jan 2006
    Location
    Leuven, Belgium
    Beans
    3,414

    Re: bash variables within sed commands not working!

    Quote Originally Posted by Mlehliw View Post
    sed "s/TEXT/$VAR/" input_file
    Code:
    sed -e s/TEXT/$VAR/ input_file

  5. #5
    Join Date
    Mar 2005
    Beans
    51

    Re: bash variables within sed commands not working!

    I came up with a solution and completely forgot about this thread, anyway if anyone's having the same problems. I changed

    sed "s/TEXT/$VAR/" input_file

    to

    sed "s|TEXT|$VAR|" input_file
    Last edited by Mlehliw; March 15th, 2008 at 05:49 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •