Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help replacing single quotes in bash script

i am trying to replace the quotes in a string in my bash script. i have a variable that will be placed in a mysql query that may possibly have a single quote in it. i want to replace any ' with \' so i can escape any quote and not screw up my query.

i tried this as a test but it doesn't work:

text="bobby's test"
echo ${text/#'/\\\'}

what am i doing wrong or is there even a better way to do this that i havent thought of? i prefer to not have to use sed or anything as well.

like image 663
user2328273 Avatar asked Jan 18 '26 05:01

user2328273


1 Answers

echo "${text//\'/\'}"

Make sure you use the double quotes on the outside, or you are going to need more escaping (ugh).

like image 106
Zombo Avatar answered Jan 19 '26 18:01

Zombo