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: Eli Schwartz
Subject: Re: `printf %q` but more human readable
Date: Sun, 14 Mar 2021 08:40:01 -0400

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\
> 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

Attachment: OpenPGP_signature
Description: OpenPGP digital signature


reply via email to

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