Escape filenames the same way Bash does it

Use printf (1):

x='a real \good %* load of c$rap'
x=$(printf '%q' "$x")
echo $x

will return

a\ real\ \\good\ %\*\ load\ of\ c\$rap

Leave a Comment