help-bash
[Top][All Lists]
Advanced

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

Re: `printf %q` but more human readable


From: Alex fxmbsw7 Ratchev
Subject: Re: `printf %q` but more human readable
Date: Sun, 14 Mar 2021 14:49:48 +0100

dont know where you read that

              Q      The  expansion is a string that is the value of
parameter quoted in a format that can
                     be reused as input.

              E      The expansion is a string that is the value of
parameter with  backslash  escape  se-
                     quences expanded as with the $'...' quoting mechanism.

              K      Produces  a  possibly-quoted version of the value of
parameter, except that it prints
                     the values of indexed and associative arrays as a
sequence of quoted key-value  pairs
                     (see Arrays above)

E states \ get escaped using $'\\'


On Sun, Mar 14, 2021 at 1:40 PM Eli Schwartz <eschwartz@archlinux.org>
wrote:

> On 3/14/21 12:52 AM, Peng Yu wrote:
> > Hi,
> >
> > I want to have something similar to `printf %q` but the result should
> > be more human-readable. I come up with the following code. Here is one
> > example to show that the result of printfbq is more human-readable
> > than `printf %q`. The idea is to reduce the use of backslashes
> > whenever possible. But human-readablilty may be a subjective matter.
> > So different people might have slightly different definitions.
> >
> > $ printfbq "$x"
> > "while true; do echo 'bash -i >& /dev/tcp/127.0.0.1/4444 0>&1' | bash;
> > sleep 5; done &"
> > $ printf '%q\n' "$x"
> > while\ true\;\ do\ echo\ \'bash\ -i\ \>\&\ /dev/tcp/127.0.0.1/4444\
> <http://127.0.0.1/4444%5C>
> > 0\>\&1\'\ \|\ bash\;\ sleep\ 5\;\ done\ \&
> >
> > The code is listed below.
> >
> > I think that there may be corner cases in which my code may not do
> > well. But I am not sure what those corner cases are.
> >
> > Does anybody have a better way to make something equivalent to `printf
> > %q` but more human-readable against all possible cases?
>
> It's just different quoting styles, but I prefer the single-quoted
> version too.
>
> GNU /usr/bin/printf does this
>
> $ /bin/printf "$x"
> while true; do echo 'bash -i >& /dev/tcp/127.0.0.^C4444 0>&1' | bash;
> sleep 5; done &
>
> And for ${parameter@operator}
>
> $ echo "${x@Q}"
> $'while true; do echo \'bash -i >& /dev/tcp/127.0.0.^C4444 0>&1\' |
> bash;\nsleep 5; done &'
> $ echo "${x@E}"
> while true; do echo 'bash -i >& /dev/tcp/127.0.0.^C4444 0>&1' | bash;
> sleep 5; done &
>
>
> (This confuses me since the manual stated that E produces $'' style
> escaping and Q produces single quoted escaping when used for an array...)
>
>
> --
> Eli Schwartz
> Arch Linux Bug Wrangler and Trusted User
>
>


reply via email to

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