[mdlug] Need help with bash array usage

Brian brian at dangerbacon.com
Tue Jul 8 22:34:57 EDT 2008


On Tue, Jul 8, 2008 at 9:45 PM, Jeff Hanson <jhansonxi at gmail.com> wrote:

> Sample script and directories attached.  I'm trying to find files with
> a specific filename extension, containing a specific string, in a
> directory tree where directory and file names may contain spaces, then
> put it into an array with one complete path per element.  I can obtain
> a list with quoted paths and use command substitution to load it into
> the array but the assignment ignores the quotes and each element gets
> a space-delimited substring.  I think it's a word-splitting problem
> but am not sure how to fix it.


I suspect you're right.  Files with spaces are the bane of every shell
script that has to deal with them.  The only trick I know of to deal with
this type of situation is pipe your data into a while loop to keep the
spaces from from splitting your data to separate elements.

Something like this:
find . -type f -iname '*.ext' | while read FILE_NAME; do
   #do stuff
done

-- 
Brian
"It's not stupid, it's advaaanced!" - Tallest Purple



More information about the mdlug mailing list