user2328273
user2328273

Reputation: 968

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.

Upvotes: 3

Views: 3995

Answers (1)

Zombo
Zombo

Reputation: 1

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

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

Upvotes: 5

Related Questions