Directories having spaces

Hi
I have a list file with complete path of files and there are directories with spaces. Now when i read the list file, read the line and read the file it says no file found because of the space. How can I get through this issues

for file in $(<list_file.txt)
do
    first_line=`head -1 "$file"`
	echo "$file , $first_line" >> header_info.txt
done

Thanks

Try:

while read -r file
do	read -r first_line < "$file"
	printf '%s , %s\n' "$file" "$first_line"
done < list_file.txt >> header_info.txt

Hi
Thanks for responding. I am getting below error

': not a valid identifierile

It seems likely that when you put the script I suggested in a file, you used an editor with the line terminator set to <carriage-return><linefeed> (as you would expect in DOS files) instead of just the <linefeed> (AKA <newline>) character that is required by the shell.

If you rremove the <carriage-return> characters from the file containing the commands I suggested, it should work.

Do you have any requirement about which shell to use? If you run the script under zsh, it would work with a small modification:

for file in "$(<list_file.txt)"
do
     first_line=`head -1 "$file"`
     echo "$file , $first_line" >> header_info.txt 
done

That is a zsh variation on dangerous use of backticks and should be avoided.

[edit] Come to think of it, I'd be surprised if it split on lines rather than whitespace either.

In short, whenever the idea is "cram entire file or stream into single variable, then process one by one", you can leave out the 'cram entire file into variable' part and still process one by one with far less danger of hitting the maximum size of variables or process memory.

Of course there is a risk that the resulting line becomes too long, and a different form - in zsh, it would mayb zargs - should be used. However, since the OP already had used this construction, I assumed he was aware of this, and focused in my answer on the splitting issue.

In zsh, splitting works as announced, provided that you use quotes around the $(....). Zsh often differs from bash when it comes to constructing arguments out of shell variables.

I had a look at that page and I think there is a mistake there in the sense that ARG_MAX is related to the arguments passed to a new process, which does not apply in the case of a for loop.

There is of course a "danger" (if you want to call it that) of unintended field splitting, so the construct is still wrong (unless it is zsh apparently), but not for the reason mentioned on that page...

Works fine in posix shells: dash, ksh, zsh, bash, ...

# this solution no problem even input lines include some special chars, $IFS chars and so on.
while read file
do
    first_line=$(head -1 "$file")   # or use read
    echo "$file , $first_line" >> header_info.txt
done < list_file.txt >> header_info.txt