|
Search Forums:
|
|||||||
| Forums | Register | Forum Rules | Linux and Unix Links | Man Pages | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
Setting IFS
Quote:
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
|
|||
|
|||
|
Thank you for this suggestion. It was very helpful for me.
|
| Sponsored Links | ||
|
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|