K
K
Konstantin Rudenkov2014-09-23 16:30:56
linux
Konstantin Rudenkov, 2014-09-23 16:30:56

How to automatically escape characters in a string variable in shell?

Hello! Can you tell me the name of the utility that, having received a string as input, will give the same answer, but with escaped characters?
for example, I get a certain URI, conjure SED over it, respectively, the sed falls with an error.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Rudenkov, 2014-09-25
@rudenkovk

Here's the solution that worked for me:

function sedeasy {
  sed -i "s/$(echo $1 | sed -e 's/\([[\/.*]\|\]\)/\\&/g')/$(echo $2 | sed -e 's/[\/&]/\\&/g')/g" $3
}

Z
Zr, 2014-09-24
@Zr

$ printf '%q\n' 'foo\bar? baz!'
foo\\bar\?\ baz\!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question