使用带有空格的文件名转义变量

Escape variables with filenames that have spaces

我正在使用这一行来批处理我的 pngcrush,但我的文件包含空格,这些空格被逐字地放入 $line,由于它们不是有效路径而被跳过:

ls *.png | while read line; do pngcrush -brute $line compressed/$line; done

如何让 $line 变成转义符,就像这个文件名 Button - Users.png 会被 Button\ -\ Users.png 替换?

只需将变量括在双引号中。这是一个很好的做法:

ls *.png | while read line; do pngcrush -brute "$line" "compressed/$line"; done

Don't parse the output of ls。在这里使用 for 循环。

for f in *.png; do
    pngcrush -brute "$line" compressed/"$line"
done