$ 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)