For loops, Spaces in values

Cameron Simpson cs at zip.com.au
Tue Nov 28 08:51:05 CST 2006


On 28Nov2006 10:58, Biedermann, Frank (SSABSA) <frank at ssabsa.sa.gov.au> wrote:
| > IFS I think..
| 
| That's what it is in awk - not sure if it's the same in Bash. Ah, it is
| according to a quick Google:
| 
| # Set IFS to newline only. See BASH(1) manpage for details on IFS.
| IFS=$'\n'

Or, portably:

IFS='
'

That $'...' syntax is some weird bash specific i18n or char encoding
thing as I recall.

Generally, this:

  for file in `listdirs`
  do
    ...

is better written:

  listdirs \
  | while read -r file
    do
      ...

This avoid all the messing with $IFS; IFS affects stuff too...
intrusively to be routinely fiddled with. It does mean the loop's in a
subshell, which can affect things.

Of course, if you're really unlucky there can be newlines in the
filenames too:-)
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

Hmmm... I thought I was smart, but I have to look up planaria in the
dictionary.     - ronniejr at world.std.com (Ronnie Jr.)


More information about the linuxsa mailing list