Vernard
Vernard

Reputation: 477

Bash script, replace text in file?

I currently have

echo -n "MySQL Database Name:  "; read MyVar; sed -i 's/database_name_here/$MyVar/g' config.php

It partially works, it prompts me to enter a database name. Once entered it updates the file but it's still putting $MyVar instead of what's typed.

echo -n "MySQL Database Name:  "; read MyVar; echo "$Myvar"

^ works just fine.

Any ideas?

UPDATE:

echo -n MySQL Database Name: ; read MyVar; sed -i s/database_name_here/$MyVar/g config.php
# ////
echo -n MySQL Username: ; read usr; sed -i s/username_here/$usr/g config.php
# ////
echo -n MySQL Password: ; read blackberg; sed -i s/password_here/$blackberg/g config.php

If it's just one line on the bash it works and replaces the value. But when I have the above, it works partially but replaces the values with blank.

Any tips?

Upvotes: 3

Views: 1421

Answers (1)

johnshen64
johnshen64

Reputation: 3884

sed -i "s/database_name_here/$MyVar/g" config.php

to ask shell to interpret the variable, you need to use double quotes instead.

Upvotes: 4

Related Questions