help-bash
[Top][All Lists]
Advanced

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

Re: string escaping in bash


From: Alex fxmbsw7 Ratchev
Subject: Re: string escaping in bash
Date: Fri, 12 Mar 2021 17:40:06 +0100

i see
well manual chaining then

var=b\\c\\tab\\t\\bnewline\\n\\\\n var=${var//\\n/$'\n'}
var=${var//\\t/$'\t'} vvar=${var//\\/\\\\} ; printf %s "$var"

but that doesnt cover \\ backslashes yet, eg \\n for \n liteeral
no idea of your needs, maybe you can describe in more detail

On Fri, Mar 12, 2021, 17:07 Peng Yu <pengyu.ut@gmail.com> wrote:

> No. `printf %b '\a'` prints a bell character. But I still want a slash
> and the character "a". Basically, I only want to treat \\ \n and \t
> specially. All others should be treated literally.
>
> On 3/12/21, Alex fxmbsw7 Ratchev <fxmbsw7@gmail.com> wrote:
> > you may be looking for
> > # printf %b "$str"
> > to interpret the string by prrintf
> >
> > or mass chained singular statements, like you showed or similiar
> >
> > On Fri, Mar 12, 2021, 16:05 Peng Yu <pengyu.ut@gmail.com> wrote:
> >
> >> Hi,
> >>
> >> 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
> >> replaced as a newline character, the string "\t" (a slash and the
> >> letter "t") should be replaced as a tab character, and "\\" (two
> >> consecutive slashes should be replaced with a single slash. All other
> >> characters and their preceding slash (if there is) should remain as
> >> is.
> >>
> >> If I use a multi-string-replacement strategy, it will not be robust.
> >> For example, if I do it in the order 1) \\ -> \, 2) \n -> newline, \\n
> >> will not be replaced correctly. The result should be "\n" (a slash
> >> followed by the letter "n").
> >>
> >> $ x='\\n'; x=${x//\\\\/\\}; x=${x//\\n/$'\n'}; declare -p x
> >> declare -- x="
> >> "
> >>
> >> Does anybody have a robust way to implement this in bash?
> >>
> >> --
> >> Regards,
> >> Peng
> >>
> >>
> >
>
>
> --
> Regards,
> Peng
>


reply via email to

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