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'.
> Does anybody have a robust way to implement this in bash?
function unescape-helpbash202103-47 {
local s=$1
s=${s//z/z0}
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