0

I have a find result saved in a variable and I am protecting the spaces in filenames by adding a single quotes around the output. So the for loop works flawlessly.

My problem is when I intend to grep one of those files, it says "File does not exist" because is literally taking the single quotes as part of the filename.

How can I overcome this??

all_files=$(find . -type f -printf "'%p'")

for file in $all_files
do
       grep 'hello' $file ### this says "file.not found because of the single quotes
done
  • 1
  • You are missing a single quote after %p. 2) It is better to double quote the entire declaration of the variable like this: "$(find . -type f -printf "'%p")"
  • – Nasir Riley Mar 24 '19 at 02:31
  • @NasirRiley I corrected this. It's not my code of course. It's only a simplification of the problem. If I surround everything in double quotes, how does that help to solve my problem? Can you clarify? – Matias Barrios Mar 24 '19 at 02:41
  • 2
    Really it all goes bad as soon as you assign find results to a string - for a robust solution see Loop through find command results that have been added to an array? filenames with gaps treats as 2 entries – steeldriver Mar 24 '19 at 03:04
  • @steeldriver I see your point. Thanks! – Matias Barrios Mar 24 '19 at 03:23
  • 1