Not entirely sure what you are trying to do with
sed there. Is this what you are looking for?
printf "\x27 \x60\n"
# ' `
printf "%x %x\n" "''" "'\`"
# 27 60
Taken from BashFAQ 071
sed is perfectly fine with either
' in its regex. Where you are running into problems is getting those characters through the shell interpreter since both single quote and backtick have special meaning to the shell, and must be quoted if they are to make it to sed without bash throwing a syntax error or mangling the input.
There are three ways to quote special characters: single quote
', double quote
", and backslash
\. They all behave a bit differently.
' quotes everything except itself, so you can use this to quote
` but not
" quotes everything except
$ (irrelevant here), so you can use
" to quote
' but not
\ quotes everything so you could use that for both. It is possible to nest these methods as long as you keep the quoting rules straight (they are interpreted from left to right).
To make things more concrete, suppose you wanted
s/`'/replacement/ in your
sed regex. There are many ways to do this. Two different examples are given below.
# concatenation of single-quoted s/`, backslash-quoted ', and
# single-quoted remainder of the command
sed -e 's/`'\''/replacement/'
# one double-quoted string quotes the single-quote, but an
# additional backslash is needed to quote the backtick
sed -e "s/\`'/replacement/"
Just keep the quoting rules in mind and use the most readable version that gets the job done. See the
QUOTING section in the bash manual for a complete explanation and reference.
As a final note, I'd avoid using the special
\o etc. escapes because
- They are GNU extensions to
sed which may be less portable than properly quoted patterns.
- It's a bit harder to read. How many people know the ASCII values for
` off the top of their heads?