triou
triou

Reputation: 47

How to quote string of special characters?

In a shell script I would like to quote this string of special characters \'%"\"'\ How do I escape the quotes/backslashes inside the string ?

Upvotes: 1

Views: 809

Answers (1)

Tom Fenech
Tom Fenech

Reputation: 74635

If you use single quotes around the whole string, then the only thing you need to worry about is replacing every ' with '"'"':

$ string='\'"'"'%"\"'"'"'\'
$ echo "$string"
\'%"\"'\

This means:

  • ' close the previous single-quoted string
  • "'" a new double-quoted string, containing a single quote
  • ' open a new single-quoted string

The shell concatenates adjacent strings, so you get a single quote where you want it. You can replace the middle part by \' but personally I think that's more confusing!

Upvotes: 2

Related Questions