0

Suppose in a directory there are two files

file1.jpg 
file 2.jpg

Here's my code to loop through the file:

for i in $dir/*
do 
   type=`exiftime -tg $i | cut -c-5`
done

The issue I have is when the loop looks at "file 2.jpg", it treats it as two files because of the space, "file" and "2.jpg". How do I get it to treat the file as one file?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user116064
  • 67
  • 2
  • 8
  • 1
    Always *Quote* your variables to prevent *word-splitting* (e.g. `"$i"`). Yes, quotes *are* permitted (and necessary) within *command substitution* (e.g. `$(....)` or the same with *backticks*) – David C. Rankin Jan 28 '16 at 04:23

1 Answers1

4

Enclose $i in double quotes: "$i"

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • For background, see also http://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-variable – tripleee Jan 28 '16 at 04:25
  • Quote `$dir` too; `for i in "$dir"/*`. You cannot quote the asterisk because that will inhibit wildcard expansion. – tripleee Jan 28 '16 at 04:27
  • Thanks for the help. I have another question, when I run exiftime -tg | cut -c-10 on an invalid pictures, it returns the entire message instead of just the first ten characters. Why does the cut not work here? – user116064 Jan 28 '16 at 04:34
  • 1
    @user116064: If you have another question, post another question. – Keith Thompson Jan 28 '16 at 04:56