sed command not accepting variable in shell script

I am using a shell script in fedora linux. While calling to the shell I am also passing an argument (var1=0.77) like shown below

sh gossip.sh var1=0.77

in the shell following command is written (which doesn't work)

sed - i -e 's@prob=@prob="$var1";//@g' file.txt

Actually i want the above command to be interpretted as

sed - i -e 's@prob=@prob=0.77;//@g' file.txt

However the above command is working fine and doing following

file.txt(INPUT)

prob=0.2;

file.txt(OUTPUT)

prob=0.77;//0.2;

That is the output i want using variable (i.e. $var1) passed as an argument through shell invoking command (i.e. sh gossip.sh var1=0.77). Please help.

Is this a homework assignment?

What shell are you using?

The sed command that you have shown us would give you a syntax error, not behave as you have suggested?

What is in gossip.sh besides the sed command you've shown us?

I have attached the sample shell and sample text file. These are doing the same as i mentioned in my post without any error.

You didn't answer the question: Is this a homework assignment? If this is a homework assignment, you need to refile this problem in the Homework and Coursework Questions forum and fill out the homework template completely from the Rules for Homework and Coursework Questions Forum.

If this isn't a homework assignment, please explain how this code will be used.

There is a HUGE difference between the sed command you posted before:

sed - i -e 's@prob=@prob=0.77;//@g' file.txt

and the code you attached to your last message:

sed -i -e  's@prob=@prob=0.77;//@g' test3.txt

(and I'm not referring to the fact that you're editing a different file).

This is not an homework assignment. I am implementing a routing protocol for which i need to run the simulation many times with changed values (Changing the value of variable $var1).

So, would you mind to answer/help please?

You cannot pass variables like this to shell (but to awk - are you mixing it up?).
Instead you pass the values (command arguments).
In the script they are available as positional parameters $1 $2 ...

In addition to what MadeInGermany said, shell variables are not expanded inside single quotes (even if you have double quotes inside the single quotes). You could change gossip.sh to:

sed -i -e "s@prob=@prob=$var1;//@g" file.txt

(note the changes of single quotes to double quotes and the removal of the previous double quotes) and invoke it as:

sh var1=0.77 gossip.sh

(putting var1 into the environment in which gossip.sh runs) or, more conventionally, change gossip.sh to:

var1="$1"
sed -i -e "s@prob=@prob=$var1;//@g" file.txt

or, more simply:

sed -i -e "s@prob=@prob=$1;//@g" file.txt

and invoke it as:

sh gossip.sh 0.77

Thanks Dear Don Cragon!

It was really helpful. I tried below and it is working.

sed -i -e "s@prob=@prob=$1;//@g" file.txt

I will also try the rest of ways that you explained. Thanks again.