Escape filenames the same way Bash does it

后端 未结 6 1400
攒了一身酷
攒了一身酷 2020-11-30 06:14

When I use the \"tab\" key in bash (when you have started to type the filename and you want it to complete), bash escapes the filename correctly, a

6条回答
  •  攒了一身酷
    2020-11-30 06:33

    The solution from "sehe" works fine, in addition, you can also use double quotes (") instead of single apostrophe (') to by able to use variables:

    x="a real \good %* load of crap from ${USER}"
    echo $(printf '%q' "$x")
    

    Of course the string may not contain $ or " itself or you have to escape those manulally by splash \$.

提交回复
热议问题