user1467267
user1467267

Reputation:

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?

Upvotes: 0

Views: 200

Answers (2)

chepner
chepner

Reputation: 531165

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

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

Upvotes: 6

Vrata Blazek
Vrata Blazek

Reputation: 464

Simply enclose variables in double-quotes. It's a good practice ever:

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

Upvotes: -2

Related Questions