help-bash
[Top][All Lists]
Advanced

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

Re: string escaping in bash


From: Peng Yu
Subject: Re: string escaping in bash
Date: Fri, 12 Mar 2021 10:07:49 -0600

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]