Using sed with bash variables

Hi Guys

I have another problem I'm trying to solve and hope that some one can help me here.

This is the scenario:

I have a file and I want to add a line on the 3rd line of the file using a bash script. but instead its adding the the bash variable $WEBSITE.

Below is the bash script I'm using.

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11

echo "what is the website ?"
read WEBSITE
if grep -q $WEBSITE "/etc/squid3/acls/allowed-sites"
then
        echo "$WEBSITE already exists"
else
        echo "adding website $WEBSITE"
        sed -ie '3i\$WEBSITE\' /etc/squid3/acls/allowed-sites
        echo "restarting squid"
fi

I have tried a few different ways of doing this with sed but nothing works.

Please help.

Please use code tags as required by forum rules!

The single quotes prevent the variable from expansion, so the literal name is inserted. Try using double quotes instead, and don't escape the $ with the \ . Some sed s insist on a line break before the insertion text.

I changed the code to this

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11

echo "what is the website ?"
read WEBSITE
if grep -q $WEBSITE "/etc/squid3/acls/allowed-sites"
then
        echo "$WEBSITE already exists"
else
        echo "adding website $WEBSITE"
        sed -i -e "3i$WEBSITE\" /etc/squid3/acls/allowed-sites
fi

and now im getting this error

/usr/local/sbin/add-website.sh: line 19: unexpected EOF while looking for matching `"'
/usr/local/sbin/add-website.sh: line 22: syntax error: unexpected end of file

Not sure where to go from here

my crystal ball says there's no matching double-quote on line 19 of your script.
sed -i -e "3i$WEBSITE\" /etc/squid3/acls/allowed-sites
What exactly is the purpose of \" ?

if u use the command like this

 sed -i -e "3i/$WEBSITE/g" /etc/squid3/acls/allowed-sites

then i get this in the file

/mm.mm/g

Im new to using sed so i dont really know what all the quotes and things mean yet.

So that would mean that the variable WEBSITE contained the text mm.mm . Could that be?

That is correct

---------- Post updated at 04:43 PM ---------- Previous update was at 04:36 PM ----------

I managed to fix it. The old code was

sed -ie '3i$WEBSITE\' /etc/squid3/acls/allowed-sites

The correct code to allow the variable to work is

sed -i -e '3i'$WEBSITE'\' /etc/squid3/acls/allowed-sites

So now if i run the script and use debian.com as the input text for the variable then it adds the text debian.com to the file allowed-sites.

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11

echo "what is the website ?"
read WEBSITE
if grep -q $WEBSITE "/etc/squid3/acls/allowed-sites"
then
        echo "$WEBSITE already exists"
else
        echo "adding website $WEBSITE"
        sed -i -e '3i'$WEBSITE'\' /etc/squid3/acls/allowed-sites
        echo "restarting squid"
        /etc/init.d/squid3 restart
fi

Thanks for the help guys !.