add literal string with simple and double quotes to a file
by vincix from LinuxQuestions.org on (#53PH1)
I'm trying to add the following string to a file:
Code:sed 's|\$MYSQL_PASSWORD|'"$MYSQL_PASSWORD"'|'I'm using the simple and double quote syntax so that sed would use the MYSQL_PASSWORD variable (in the second part; the first part is literally $MYSQL_PASSWORD) when sed is run in the file I'm trying to add the line to.
What I've tried to far is:
Code:echo sed 's|\$MYSQL_PASSWORD|'"\$MYSQL_PASSWORD"'|' > fileBut unfortunately it drops the quote altogether:
Code:sed s|\$MYSQL_PASSWORD|$MYSQL_PASSWORD|Somewhat closer is:
Code:echo sed 's|\$MYSQL_PASSWORD|'"'"\$MYSQL_PASSWORD"'"'|'But this drops the double quotes:
Code:sed s|\$MYSQL_PASSWORD|'$MYSQL_PASSWORD'|This is becoming a little bit retarded, so if there are more elegant solutions, I'm looking forward to it :)
Ok, I was actually able to find a horrible solution:
Code:echo sed \''s|\$MYSQL_PASSWORD|'"'\""\$MYSQL_PASSWORD"'"\"'|'\'But the question remains: is there any other way to achieve this?
Thanks!


Code:sed 's|\$MYSQL_PASSWORD|'"$MYSQL_PASSWORD"'|'I'm using the simple and double quote syntax so that sed would use the MYSQL_PASSWORD variable (in the second part; the first part is literally $MYSQL_PASSWORD) when sed is run in the file I'm trying to add the line to.
What I've tried to far is:
Code:echo sed 's|\$MYSQL_PASSWORD|'"\$MYSQL_PASSWORD"'|' > fileBut unfortunately it drops the quote altogether:
Code:sed s|\$MYSQL_PASSWORD|$MYSQL_PASSWORD|Somewhat closer is:
Code:echo sed 's|\$MYSQL_PASSWORD|'"'"\$MYSQL_PASSWORD"'"'|'But this drops the double quotes:
Code:sed s|\$MYSQL_PASSWORD|'$MYSQL_PASSWORD'|This is becoming a little bit retarded, so if there are more elegant solutions, I'm looking forward to it :)
Ok, I was actually able to find a horrible solution:
Code:echo sed \''s|\$MYSQL_PASSWORD|'"'\""\$MYSQL_PASSWORD"'"\"'|'\'But the question remains: is there any other way to achieve this?
Thanks!