Hong Wei
Hong Wei

Reputation: 1407

Escaping spaces for a list of file paths

I am new to shell scripting. I am trying to loop through a list of file paths from a find and count the number of lines for each file in the list. Some of the file paths contain spaces so they are interpreted as separate elements in the for loop instead of a single element.

When I run the following:

for filepath in `find . -name '*.m'`; do echo $filepath; done;

I see that some of the paths with spaces are printed across multiple lines instead of being printed on the same line.

How do escape the spaces for each filepath?

Upvotes: 0

Views: 89

Answers (1)

Ruslan Osmanov
Ruslan Osmanov

Reputation: 21492

I am trying to loop through a list of file paths from a find and count the number of lines for each file in the list.

The following is the proper way of doing this:

find . -type f -name '*.md' -exec wc -l {} \;

The for .. in loop accepts a list of IFS-separated words. IFS contains space by default, so the spaces within filenames actually split the filenames into separate words.

Upvotes: 1

Related Questions