Help With 'for' Loop

Will Maier willmaier at ml1.net
Fri Oct 14 13:05:37 PDT 2005


On Fri, Oct 14, 2005 at 12:33:07PM -0700, Drew Tomlinson wrote:
[...]
> Yet 'echo $i' only returns "/multimedia/Pictures/1998", stopping
> at the first space.  Is it possible to get 'i' to represent the
> whole string that 'find' returns?  If so, how?

Bourne-style for loops use <space> as the delimiter by default. To
change this behavior, modify the IFS variable (which is mentioned
but not explained in the sh manpage):
    
    $ OLDIFS=$IFS	# probably want to remember this value
    $ IFS=:
    $ export $IFS
    $ for i in $PATH; do
	echo $i
    done
    /home/will/bin
    /usr/local/sbin
    /usr/local/bin
    /usr/sbin
    /usr/bin
    /sbin
    /bin
    /usr/X11R6/bin
    /opt/
    /usr/games/
    $ IFS=$OLDIFS	# set it back to normal
    $ export $IFS	
    $ for i in $PATH; do
	echo $i
    done
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin:/opt/:/usr/games/

-- 

o--------------------------{ Will Maier }--------------------------o
| jabber:..wcmaier at jabber.ccc.de | email:..........wcmaier at ml1.net |
| \.........wcmaier at cae.wisc.edu | \..........wcmaier at cae.wisc.edu |
*------------------[ BSD Unix: Live Free or Die ]------------------*



More information about the freebsd-questions mailing list