News for hackers. Stuff that matters.
Re:Escaping strings in Bash using !:q by ctur on Friday October 02, 2020 @02:10AM (#24659461)
Even better -- use this script, paste anything into stdin. It will come back out quotable and pasteable into a shell.

  #!/bin/bash
  
  printf '%q' "$(cat)"
  echo
Re:Escaping strings in Bash using !:q by Tepix on Friday October 02, 2020 @03:04AM (#24659808)
Even better, put this in your ~/.bashrc

    function bashquote () {
        printf '%q' "$(cat)"
        echo
    }
Re:Escaping strings in Bash using !:q by asicsp on Friday October 02, 2020 @04:30AM (#24660392)
Golfed one-liner

    bashquote() { printf '%q\n' "$(cat)" ; }
Re:Escaping strings in Bash using !:q by KMnO4 on Friday October 02, 2020 @07:25AM (#24661458)
You should contact your administrator if you are still paying per-line for your .bashrc
Re:Escaping strings in Bash using !:q by dotancohen on Friday October 02, 2020 @08:22AM (#24661861)
I think he's just saving on keystokes because those Model M's are getting hard to find.
Re:Escaping strings in Bash using !:q by Tepix on Friday October 02, 2020 @02:40PM (#24665653)
Can't we get rid of cat somehow?
Re:Escaping strings in Bash using !:q by kps on Friday October 02, 2020 @11:07PM (#24669168)
Yes,

    bashquote() { printf '%q\n' "$(</dev/stdin)"; }
Bash treats the string "/dev/stdin" as magic, so that works even if /dev/stdin doesn't exist. However, bash (unlike ksh) spawns a subshell for $(</dev/stdin), so using cat is actually lighter. (Also, it's not clear to me why $(<&0) doesn't work in bash.)
Re:Escaping strings in Bash using !:q by klysm on Friday October 02, 2020 @03:32AM (#24660022)
Is the echo there just for the newline? Why not slap a \n in there and make it a one liner?
Re:Escaping strings in Bash using !:q by birdyrooster on Friday October 02, 2020 @12:48PM (#24664482)
Or on Mac, just run this script and it will modify your clipboard

  #!/bin/bash
  printf '%q' "$(pbpaste)" | pbcopy
Re:Escaping strings in Bash using !:q by twic on Friday October 02, 2020 @04:44AM (#24660493)

  $ echo "text with spaces" | bash -c 'printf "%q" "$(cat)"'; echo
  text\ with\ spaces
For me, this escapes spaces with backslashes; the example in the article escapes them by quoting the whole string. Is this a difference in bash versions, or do these uses differ somehow?

(I replaced the single quotes with double so i could pass this to bash -c, but i get the same result if i use your code verbatim as a script)

Re:Escaping strings in Bash using !:q by asicsp on Friday October 02, 2020 @05:27AM (#24660710)

    text\ with\ spaces
is same as

    'text with spaces'
See https://mywiki.wooledge.org/Quotes
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.