[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Bash scripts and multi-word filenames



On 2006-08-05 15:45:26 +1000, John O'Hagan wrote:
> FILES=$( ls SOMEWHERE )  #or find or grep, etc
> 
> for FILE in $FILES; do SOMETHING; done
> 
> I often find that if there are spaces in the names of anything in
> $FILES, the execution of SOMETHING occurs separately for each word
> in the name, producing unexpected results.
> 
> Quoting doesn't seem to help. Are there ways to deal with this?

Use zsh:

$ zsh -f
ay% ls -1
ab cd
ef gh
ay% for file in *; do printf "File: %s\n" $file; done
File: ab cd
File: ef gh

With find, you have the -print0 option, that is safe.

Note that solutions based on the \n separator will not always work
as filenames can also contains this character.

-- 
Vincent Lefèvre <vincent@vinc17.org> - Web: <http://www.vinc17.org/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.org/blog/>
Work: CR INRIA - computer arithmetic / SPACES project at LORIA



Reply to: