Search code examples
bashpngcrush

Escape variables with filenames that have spaces


I'm using this line to batch my pngcrush, yet my files contain spaces, which get's placed literally into $line, making them get skipped as they aren't valid paths:

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

How can I make $line become escape like this filename Button - Users.png would be replaced with Button\ -\ Users.png?


Solution

  • Don't parse the output of ls. Use a for loop here instead.

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