News for hackers. Stuff that matters.
Re:Escaping strings in Bash using !:q by twic on Friday October 02, 2020 @06:33AM (#24661112)
It's plainly not the same, you can see that by looking at it. It encodes the same text, but it encodes it a different way.

And the difference is not negligible. With quotes, i can type or paste more into the middle, and the string is still valid. With escapes, i have to be careful to escape the added text. It's significantly more ergonomic to use quoted text.

Re:Escaping strings in Bash using !:q by zeroimpl on Sunday October 04, 2020 @04:51PM (#24682169)
Yes the example in the article and parent comments ought to be referred to as escaping, not quoting. Most of the time, quoting with single quotes leads to something far easier on the eyes.

Here's an example function for quoting from stdin:

    function bashquotesingle() { 
        printf "'"; 
        sed "s/'/'\\\\''/g"; 
        printf "'"; 
    }
Though it does have a bug, at least on macOS, where sed adds a trailing newline. (See https://stackoverflow.com/questions/13325138/why-does-sed-ad...)
Re:Escaping strings in Bash using !:q by kps on Friday October 02, 2020 @10:20AM (#24662860)
ksh's %q prefers quotes; for your example it prints 'text with spaces', and for the article's example,

    $'This string \'has single\' "and double" quotes and a $'
vs bash's %q,

    This\ string\ \'has\ single\'\ \"and\ double\"\ quotes\ and\ a\ \$
Re:Escaping strings in Bash using !:q by eMSF on Friday October 02, 2020 @06:53AM (#24661252)
If you intend to type or paste quotes, you still need some additional escaping. And on the other hand, most substrings of (non-quoted) escaped text are themselves properly escaped.