help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: string escaping in bash


From: Koichi Murase
Subject: Re: string escaping in bash
Date: Sat, 13 Mar 2021 20:18:18 +0800

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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]