How to escape all special characters?

I have an application which I am integrating with that accepts the password via a CLI. I am running in to issues with passwords that contain special characters. I tried to escape them all, but I ran in to an issue where I cannot escape the characters

  1. '
  2. [
  3. ]

My attempt is as follows:

$ echo '`~!@#$%^&*()-_=+{}\|;:",<.>/?' | sed 's/[`~!@#$%^&*()-_=+{}\|;:",<.>/?]/\\&/g'
\`\~\!\@\#\$\%\^\&\*\(\)\-\_\=\+\{\}\\\|\;\:\"\,\<\.\>\/\?

If I add [ or ] it obviously breaks my sed logic.
If I add ' it obviously causes errors in sed as it ruins the sed format and the unexpected ' later (which should remain)

If this is not possible, I will tell the client, all characters are acceptable, but generate passwords without ' [ or ] .
I wonder.... maybe I escape EVERY SINGLE Character? I'd have to test the app... and still not sure how I can do that with sed properly? Is that the solution... not sure.
Please let me know if you can help.
Thank you!

Does:

echo "'"'[]`~!@#$%^&*()-_=+{}\|;:",<.>/?' | sed 's/[][`~!@#$%^&*()-_=+{}\|;:",<.>/?'"'"']/\\&/g'

producing the output:

\'\[\]\`\~\!\@\#\$\%\^\&\*\(\)\-\_\=\+\{\}\\\|\;\:\"\,\<\.\>\/\?

do what you wanted?

You could also replace anything that isn't acceptable (alphanum and space) like this:

sed 's/[^a-zA-Z 0-9]/\\&/g'
1 Like