I am working in the Terminal app on a Mac machine running OS X Tiger.
In trying to teach myself the basics, I copied the following loop out of a book:
for i in $(ls -1 ~/Desktop/*.txt); do say -f $i; done
The difficulty is the spaces in the filenames, "One NOTICE.txt" and "Two README.txt". I would appreciate if someone could show me how to rewrite the loop so it works with such filenames.
There are 2 issues with the example:
First the 'ls' command will execute and return a list of files, including their spaces to the for-loop. The for-loop will see each word in the list it receives as an entry for it's loop. So a file with a space in it will be seen as 2 'words'. I still don't know why people do an 'ls' inside a for-loop, because many, if not all shells have file-globbing.
The second issue is that even if the variable 'i' will contain a filename with a space in it, it is passed without quotes to your 'say' command. The same happens here as described before. you will call the 'say' command with 2 separate 'words'. Both don't exist, except as a whole. If you want to pass a variable as a whole, please put double quotes around it.
So my solution would be:
for i in ~/Desktop/*.txt; do say -f "$i"; done