On Sat, Mar 13, 2021 at 6:18 AM Koichi Murase <myoga.murase@gmail.com>
wrote:
> 2021年3月12日(金) 23:06 Peng Yu <pengyu.ut@gmail.com>:
> > I wondering if there is a simple but robust way to implement string
> escaping.
> >
> > Specifically, the string "\n" (a slash and the letter "n") should be
> [...] All other
> > characters and their preceding slash (if there is) should remain as
> > is.
>
> I think this is `unescaping' rather than `escaping'.
You are right. It should be unescape. My function name reflects this. But
the original question was wrong in this aspect.
>
>
> > Does anybody have a robust way to implement this in bash?
>
> function unescape-helpbash202103-47 {
> local s=$1
> s=${s//z/z0}
This idea is great. It can be a general approach to solve the problem of
custom unescaping without the support of regex replacement.
>
> s=${s//'\\'/z1}
> s=${s//'\n'/$'\n'}
> s=${s//'\t'/$'\t'}
> s=${s//z1/'\'}
> s=${s//z0/z}
> result=$s
> }
> unescape-helpbash202103-47 '\\n'; x=$result
> declare -p x | cat -v
>
> Result:
>
> declare -- x="\\n"
>
> --
> Koichi
>
--
Regards,
Peng