Preventing whitespace to be a delimiter in a for loop (bash/sh)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Preventing whitespace to be a delimiter in a for loop (bash/sh)
# 1  
Old 09-12-2007
Preventing whitespace to be a delimiter in a for loop (bash/sh)

Hi,

I have a for loop which iterates over a list of strings, separated by whitespace:

Code:
$ list="1 2 3"
$ for i in $list; do echo $i; done
1
2
3

I now want to introduce some strings containing whitespace themselves ... This is straightforward if I directly iterate over the list:

Code:
$ for i in 1\ 2 3; do echo $i; done
1 2
3

However, this does not work if the list is in a variable

Code:
$ list="1\ 2 3"
$ for i in $list; do echo $i; done
1\
2
3

Is there a way to somehow make the whitespace in the variable $list special, so that the for loop does not recognize it as a delimiter?

BTW: The for loop is not written by myself, but is burried deep in a build script I do not want to change ...

Thanks in advance!

Kai Koehne
# 2  
Old 09-12-2007
You could try using set, e.g...
Code:
$ set "1 2" "3"
$ for i; do echo $i; done
1 2
3

$ set 1\ 2 3
$ for i; do echo $i; done
1 2
3

Note that it sets $1 $2 etc., so this may affect something else in your script.
# 3  
Old 03-07-2008
hey there,

Setting

IFS=\n

at your bash prompt will set the delimiter a for loop uses to a newline.
eg with default IFS:
$ cat filelist
1 2 3
a b c


$ for i in `cat filelist` ; do echo $i ; done
1
2
3
a
b
c

$ IFS=\n
$ for i in `cat filelist` ; do echo $i ; done
1 2 3
a b c


Not super relevant to the first post as you can't choose the spot of the delimiter. but may help anyone stumbling along this post looking for an answer on how to change the delimiter a for loop is using.
# 4  
Old 02-20-2009
Setting IFS

Quote:
Originally Posted by ConSeannery
hey there,

Setting

IFS=\n

at your bash prompt will set the delimiter a for loop uses to a newline.
In bash, I think this should be:
IFS=$'\n'

Also, you may want to store the old value of the environment variable IFS as overwriting it can potentially cause conflicts with other processes that use it.

-FJB
# 5  
Old 05-15-2009
Quote:
Originally Posted by filmjbrandon
In bash, I think this should be:
IFS=$'\n'

Also, you may want to store the old value of the environment variable IFS as overwriting it can potentially cause conflicts with other processes that use it.

-FJB
Thank you for this suggestion. It was very helpful for me.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to loop file data based on delimiter

My file has data that looks like below: more data.txt I wish to display each string seperated by a delimiter : Expected output: I tried the below but I m not getting every split string on a new line. #!/bin/bash for i in `sed 's/:/\\n/g' data.txt`; do echo -n... (2 Replies)
Discussion started by: mohtashims
2 Replies

2. Shell Programming and Scripting

Specifying IFS delimiter in while loop

i have data that is delimited with #x#: file1#x#file2#x#file3 file4#x#file5#x#file6 data is stored in a variable called ALLMYDATA: echo "${ALLMYDATA}" | while IFS="#x#" read -r line junk do echo ${line} done it appears IFS does not allow the specification of more than one... (5 Replies)
Discussion started by: SkySmart
5 Replies

3. Shell Programming and Scripting

REGEX to separate paths by whitespace and do a loop

I am trying to do in a single line to take a list of paths separated by whitespace and then loop thru all the paths that were wrote but my regex is not working, I have echo {3} | sed 's/ //g' | while read EACHFILE do ..... But for some reason is only taking always the first path that I... (7 Replies)
Discussion started by: jorgejac
7 Replies

4. Shell Programming and Scripting

BASH scripting - Preventing wget messed downloaded files

hello. How can I detect within script, that the downloaded file had not a correct size. linux:~ # wget --limit-rate=20k --ignore-length -O /Software_Downloaded/MULTIMEDIA_ADDON/skype-4.1.0.20-suse.i586.rpm ... (6 Replies)
Discussion started by: jcdole
6 Replies

5. Shell Programming and Scripting

How to match (whitespace digits whitespace) sequence?

Hi Following is an example line. echo "192.22.22.22 \"33dffwef\" 200 300 dsdsd" | sed "s:\(\ *\ \):\1:" I want it's output to be 200 However this is not the case. Can you tell me how to do it? I don't want to use AWK for this. Secondly, how can i fetch just 300? Should I use "\2"... (3 Replies)
Discussion started by: shahanali
3 Replies

6. Shell Programming and Scripting

Preventing an endless loop with recursive grep

When finding a string in files within a directory, one can use this: grep -r "searchstring" dir/subdir/ > listofoccurrences.txt For brevity sake one can enter the intended directory and use this: grep -r "searchstring" . > listofoccurrences.txt which as I found out leads to an endless loop,... (2 Replies)
Discussion started by: figaro
2 Replies

7. Shell Programming and Scripting

Whitespace in filenames in for loop in bash script

I'm trying to search all .odt files in a directory for a string in the text of the file. I've found a bash script that works, except that it can't handle whitespace in the filenames. #!/bin/bash if ; then echo "Usage: searchodt searchterm" exit 1 fi for file in $(ls *.odt); do ... (4 Replies)
Discussion started by: triplemaya
4 Replies

8. Shell Programming and Scripting

Preserving whitespace in a for loop

I obviously haven't learned my lesson with shell and whitespace. find /path/to/some/where/ -name "*.pdf" | awk '{print $5}'| uniq -d results: some Corporation other Corporate junk firmx Works fine from cmdline but the whitespace turns into another FS in a for loop. for... (7 Replies)
Discussion started by: s_becker
7 Replies

9. Shell Programming and Scripting

Of bash and whitespace...

Hmmm... Bash doesn't parse whitespace with a read. lev@sys09:~$ read line; echo "$line" test test You can imagine what this does if you're using a shell script to read a list of unknown file names containing unknown spaces. lev@sys09:~$ read word1 word2; echo "$word1,$word2" 123 456... (2 Replies)
Discussion started by: lev_lafayette
2 Replies

10. Shell Programming and Scripting

while read loop preserving leading whitespace

Hi all, I've been trying to get this to work for ages to no avail. I've searched this site and googled but cannot find a satisfactory answer. I've got a while loop, like this while read line do echo "$line" done < file_name Now, my problem is that most of the lines in the file... (3 Replies)
Discussion started by: zazzybob
3 Replies
Login or Register to Ask a Question