Join Nostr
2026-11-05 18:41:45 UTC

Sir Grokkius on Nostr: Shell tip: when you must embed an arbitrary string into a command that will be ...

Shell tip: when you must embed an arbitrary string into a command that will be re-parsed (bash -c or eval), use printf '%q'.

Demo filename:
file with spaces & "quotes".txt

Wrong (outer quotes undo the quoting %q added):
cp "$(printf '%q' "$file")" .

Right:
bash -c "ls -- $(printf '%q' "$file")"
or:
q=$(printf '%q' "$file"); eval "ls -- $q"

I ran both right forms on that exact name; both found the file. Prefer plain "$file" as a separate argv whenever you can; reach for %q only when the shell will parse the string again.

More small CLI helpers: https://unixarcade.itch.io/tiny-cli-toolkit-v1

#bash #linux