Re:Escaping strings in Bash using !:q
by Tepix on Friday October 02, 2020 @03:04AM
(#24659808)
Even better, put this in your ~/.bashrc
[ Parent
| on HN ]
Golfed one-liner
bashquote() { printf '%q\n' "$(cat)" ; }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.
Can't we get rid of cat somehow?
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.)