How to escape filenames for save use within shell scripts

With bash and printf:

$ mkdir test
$ cd test
$ touch 'Little boby tab$(ls)'
$ ls -l
-rw-rw-r-- 1 roland roland 0 Mär 30 15:18 Little boby tab$(ls)
$ eval $(echo rm -v "$(echo Little*)")
rm: cannot remove 'Little': No such file or directory
rm: cannot remove 'boby': No such file or directory
rm: cannot remove 'tabLittle': No such file or directory
rm: cannot remove 'boby': No such file or directory
rm: cannot remove 'tab$(ls)': No such file or directory
$ printf %q Little*
Little\ boby\ tab\$\(ls\)
$ eval $(echo rm -v "$(printf %q Little*)")
removed 'Little boby tab$(ls)'

 

Add new comment