Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



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

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-12-2007
Registered User
 

Join Date: Sep 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old 09-12-2007
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
 

Join Date: Oct 2003
Location: 54.23, -4.53
Posts: 1,694
Thanks: 1
Thanked 61 Times in 56 Posts
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.
Sponsored Links
    #3  
Old 03-07-2008
Registered User
 

Join Date: Mar 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
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
Registered User
 

Join Date: Feb 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Setting IFS

Quote:
Originally Posted by ConSeannery View Post
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
Sponsored Links
    #5  
Old 05-15-2009
Registered User
 

Join Date: May 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by filmjbrandon View Post
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.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Of bash and whitespace... lev_lafayette Shell Programming and Scripting 2 04-13-2008 08:44 PM
Bash while loop problem Kweekwom Shell Programming and Scripting 5 07-23-2007 12:49 AM
error in bash script 'if' loop DILEEP410 Shell Programming and Scripting 2 06-06-2007 08:04 AM
Simple bash for loop problem kingdbag Shell Programming and Scripting 4 09-15-2006 01:00 AM
while read loop preserving leading whitespace zazzybob Shell Programming and Scripting 3 06-07-2004 05:36 PM



All times are GMT -4. The time now is 05:11 AM.