Dileep Dominic
Dileep Dominic

Reputation: 519

Difference in reading from file in unix

I have the file content as

bash-4.4$ cat b.txt                                                                                                                       
unix unix                                                                                                                                 
unix                                                                                                                                      
unix unix unix                                                                                                                            
linux                                                                                                                                     
linux linux 

The below script reading the content of the file one with for loop and another with while loop . But both are printing the content in two different ways. What is the reason ?

#/usr/bin/bash                                                                                                                            

echo "for loop approach"                                                                                                                  
for i in $(cat b.txt)                                                                                                                     
do                                                                                                                                        
echo $i                                                                                                                                   
done                                                                                                                                      

echo ""                                                                                                                                   
echo "while approach"                                                                                                                     
cat b.txt | while read line                                                                                                               
do                                                                                                                                        
echo $line                                                                                                                                
done       

bash-4.4$ bash aa.bash                                                                                                                    
for loop approach                                                                                                                         
unix                                                                                                                                      
unix                                                                                                                                      
unix                                                                                                                                      
unix                                                                                                                                      
unix                                                                                                                                      
unix                                                                                                                                      
linux                                                                                                                                     
linux                                                                                                                                     
linux                                                                                                                                     

while approach                                                                                                                            
unix unix                                                                                                                                 
unix                                                                                                                                      
unix unix unix                                                                                                                            
linux                                                                                                                                     
linux linux  

Upvotes: 1

Views: 59

Answers (1)

heemayl
heemayl

Reputation: 42107

In shell, word splitting occurs on the characters of IFS variables: space, tab, and newline by default.

In your for approach, you have not quoted the command substitution, $(cat b.txt), hence word splitting (and pathname expansion) is being triggered resulting in words separated by those characters of IFS as separate entities in the output.

With the while approach, read reads each whole line (up to \n), so you're getting the whole lines in the output.

Using a for loop with command substitution is always the wrong approach while reading from a file line by line. Use a while loop with read instead.

Upvotes: 4

Related Questions