I would like to output the list of items in a folder in the folowing way:
\"filename1\" \"filename2\" \"file name with spaces\" \"foldername\" \"folder name wit
To avoid hacks trying to manually add quotes, you can take advantage of printf
s %q
formatting option:
❯ ll .zshrc.d
total 112K
-rwxrwxrwx 1 root root 378 Jul 1 04:39 options.zsh*
-rwxrwxrwx 1 root root 57 Jul 1 04:39 history.zsh*
-rwxrwxrwx 1 root root 301 Jul 1 05:01 zinit.zsh*
-rwxrwxrwx 1 root root 79K Jul 1 05:19 p10k.zsh*
-rwxrwxrwx 1 root root 345 Jul 1 05:24 zplugins.zsh*
-rwxrwxrwx 1 root root 490 Jul 4 23:40 aliases.zsh*
-rw-r--r-- 1 root root 9.0K Jul 27 08:14 lscolors.zsh
-rw-r--r-- 1 root root 0 Aug 30 05:56 'foo bar'
-rw-r--r-- 1 root root 0 Aug 30 05:58 '"foo"'
❯ find .zshrc.d -exec printf '%q ' {} +
.zshrc.d .zshrc.d/history.zsh .zshrc.d/aliases.zsh .zshrc.d/zplugins.zsh .zshrc.d/p10k.zsh .zshrc.d/zinit.zsh .zshrc.d/options.zsh '.zshrc.d/"foo"' '.zshrc.d/foo bar' .zshrc.d/lscolors.zsh #
vs:
find .zshrc.d -printf "\"%p\" "
".zshrc.d" ".zshrc.d/history.zsh" ".zshrc.d/aliases.zsh" ".zshrc.d/zplugins.zsh" ".zshrc.d/p10k.zsh" ".zshrc.d/zinit.zsh" ".zshrc.d/options.zsh" ".zshrc.d/"foo"" ".zshrc.d/foo bar" ".zshrc.d/lscolors.zsh" #
Notice the file .zshrc.d/"foo"
is incorrectly escaped.
❯ echo ".zshrc.d/"foo""
.zshrc.d/foo
❯ echo '.zshrc.d/"foo"'
.zshrc.d/"foo"