1

All the questions and answers i already read are not my solution. So, all i want to do is to replace the special char ' in bash.

This works:

A="abc"
B="${A//[b]/x}"

But this does not work:

A="a'b"
B="${A//[']/\'}"

Also, i have tried:

B="${A//[']/\\'}"
B="${A//[']/\\\'}"
B="${A//[']/\'''}"
B="${A//[']/'\\''}"

But B keeps to be a'b.

2

This way works:

A="a'b"
B="${A//\'/\\\'}"

Two notes:

  1. The [] are unnecessary when only one character is inside of them
  2. You need to escape \ and ' inside of the substitution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.