The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Problem if parameter has space in it using loop pinnacle Shell Programming and Scripting 9 06-16-2009 05:24 AM
Use of For loop with Space peter35james UNIX for Dummies Questions & Answers 3 05-28-2009 05:55 AM
Moving files with space, in for loop saabir Shell Programming and Scripting 6 02-04-2009 01:30 PM
how to protect white space in for loop pondlife Shell Programming and Scripting 6 07-01-2008 08:38 AM
Storing space delimited line in var with loop? eltinator Shell Programming and Scripting 2 08-24-2007 02:09 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 4 Weeks Ago
Tuxidow Tuxidow is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 47
Escape space in for loop

I have a file with the following contents


Code:
[root@rf4003 scripts]# more hello.txt
man
hello man
whereru

The shell script i have tries to echo the contents of the file hello.txt

Code:
for i in `cat hello.txt`
do
        echo $i
done

but the output i am getting is taking the space as a new line..

Code:
[root@rf4003 scripts]# ./hello.sh
man
hello
man
whereru

Can anybody help me to echo the contents of the file
as it should be?
  #2 (permalink)  
Old 4 Weeks Ago
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,348
The use of cat is redundant (UUC), use a while loop and quote the variables:


Code:
while read i
do
  echo "$i"
done < hello.txt

  #3 (permalink)  
Old 4 Weeks Ago
Tuxidow Tuxidow is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 47

Code:
cat hello.txt | while read i
do
  echo "$i"
done

This is also working.. thanks Franklin
  #4 (permalink)  
Old 4 Weeks Ago
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,348
As mentioned, the use of cat is redundant and cost an extra process. Have a read of Useless Use of Cat Award.

Regards
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:21 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0