Escape space in for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Escape space in for loop
# 1  
Old 11-03-2009
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  
Old 11-03-2009
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  
Old 11-03-2009
Code:
cat hello.txt | while read i
do
  echo "$i"
done

This is also working.. thanks Franklin
# 4  
Old 11-03-2009
As mentioned, the use of cat is redundant and cost an extra process. Have a read of Useless Use of Cat Award.

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

For loop with space in file name

Hi All I have a source file named ABC-20150613 to 20150613.zip. I was trying to execute the below command on this source file, but its telling file is not available in that path and giving me some random file names. ls -ltr| for z in ABC-????????*to*????????*.zip do unzip $z -d done I... (5 Replies)
Discussion started by: ginrkf
5 Replies

2. Shell Programming and Scripting

awk to escape space in name

I am searching a file and not able to escape a space if $i has a space in the name. I tried escaping it with a \ and also trying to add it to allow for spaces in the search. Neither are correct as the first awk only outputs path and the second awk doesn't run. Thank you :). first awk awk... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

Need Help on For Loop to pass space separated value as one value

Hi, I am having a file say list1 with a output like below jun 12 18:23 may 20 18:23 Now i want to pass the above two values into for loop,I have written a script like this. #!/bin/bash a=`cat list1` for i in $a do echo "HI $i" done expected output: HI jun 12 18:23 (3 Replies)
Discussion started by: sumanthupar
3 Replies

4. Shell Programming and Scripting

Auto escape script to escape special chars in script args

This is a bit off the wall, but I often need to run scripts where there are argument values that contain special characters. For example, $ ./process.exe -t M -N -o temp.mol.s -i ../molfiles/N,N\',N\'\'-trimethylbis\(hexamethylene\)triamine.mol && sfile_space_to_tab.sh temp.mol.s temp.s It... (1 Reply)
Discussion started by: LMHmedchem
1 Replies

5. Shell Programming and Scripting

How to loop through space separated values?

How do I loop thru space separated values in a variable? I hate to use very complicated counter increment logic for this kind of simple problem. Expected result(using ksh) $>echo "aaa bbbb cccc" | <looping code here> var=aaa var=bbbb var=cccc $>echo "aaa bbbb cccc" | while IFS=" "... (12 Replies)
Discussion started by: kchinnam
12 Replies

6. Shell Programming and Scripting

escape space characters in loop from file

Hi Everyone! I want to build sql inserts from a list of countries/regions saved in a file. The list looks like this: United Kingdom Czech Republic ... The script I run is: while read i; do var=`expr $var + 1`; echo "INSERT INTO calltypes VALUES($var, '$i','$i');" >>... (5 Replies)
Discussion started by: linuca
5 Replies

7. Shell Programming and Scripting

Escape Space in a ssh command

The following statement does work. But the second command does not work as expected. ssh root@123.123.123.123 \\"mysqldump -h localhost -u root -pPassWord dbName -d | gzip -cf\\" | gunzip -c > database1.sql ssh root@123.123.123.123 \\"mysqlbinlog /var/log/mysql/mysql-bin.*... (0 Replies)
Discussion started by: shantanuo
0 Replies

8. Shell Programming and Scripting

Problem if parameter has space in it using loop

for cmdopts in $*;do case $cmdopts in -mode) mode="$2";shift 2 ;; -server) server="$2";shift 2 ;; -Id) Id="$2";shift 2 ;; -passwd) passwd="$2";shift 2 ;; -rmtDir) rmtDir="$2";shift 2 ;; -lcDir) ... (9 Replies)
Discussion started by: pinnacle
9 Replies

9. UNIX for Dummies Questions & Answers

Use of For loop with Space

Hi, I am trying to query the database to get the list of portfolio and for each portfolio, I am using the for loop, but the problem is some of the portfolio is having the spaces. The Code PORT=`${EFG_ISQL} -b <<-! set nocount on use ${EFG_DB} go select portId from PORTFOLIO go... (3 Replies)
Discussion started by: peter35james
3 Replies

10. Shell Programming and Scripting

Moving files with space, in for loop

Hi All I need to put a bunch of specific files in a directory (with loads of other files), into a tar archive. The best way I thought of doing this was putting the filenames into a file, reading them line by line in a for loop, and then adding them to a tar acrhive. However the filenames have... (6 Replies)
Discussion started by: saabir
6 Replies
Login or Register to Ask a Question