section "Word Splitting" - IFS

section "Word Splitting" - IFS

Post by nabi » Thu, 23 Jun 2005 05:11:12



Hi!
I am translating man 1 bash in my native languge. There is a somewhat confu-
sing explanation (at leas for me) of IFS varible in EXPANSION section, Word
Splitting subsection. I'll quote it here:

 >  Word Splitting
 >      The shell scans the results of parameter expansion, command
substitu-
 >      tion,  and  arithmetic  expansion  that  did  not occur within
double
 >      quotes for word splitting."
example:
var="a  b c"
echo $var

 >      The shell treats each character of IFS as a delimiter, and
splits the
 >      results  of  the other expansions into words on these
characters.  If
 >      IFS is unset, or its  value  is  exactly  <space><tab><newline>,
  the
 >      default, then any sequence of IFS characters serves to delimit
words.
this is clear.

 >      If IFS has a value other than the  default,  then  sequences  of
  the
 >      whitespace  characters space and tab are ignored at the
beginning and
 >      end of the word, as long as the whitespace character is in the
value

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 >      of  IFS  (an IFS whitespace character).
that means, *unless the witespace character is back in the value of IFS*

 >                                               Any character in IFS
that is
 >      not IFS whitespace, along with any adjacent  IFS  whitespace
charac-
 >      ters,  delimits  a field.
I think, I got it:
IFS=":;"
var="a:;b;::c"
echo $var   # a  b   c

Now the part that I can barely understand:
 >                                A sequence of IFS whitespace characters is
 >      also treated as a delimiter.  If the value of IFS is  null,  no
  word
 >      splitting occurs.
 >
 >      Explicit  null  arguments ("" or '') are retained.  Unquoted
implicit
 >      null arguments, resulting from the expansion of parameters that
  have
 >      no  values,  are  removed.   If a parameter with no value is
expanded
 >      within double quotes, a null argument results and is retained.
 >
 >      Note that if no expansion occurs, no splitting is performed.
Could someone, please, expand this last part, add a few examples.

Thank you. Vitaliy.

 
 
 

section "Word Splitting" - IFS

Post by Chris F.A. Johnso » Thu, 23 Jun 2005 12:37:28



> Hi!
> I am translating man 1 bash in my native languge. There is a somewhat confu-
> sing explanation (at leas for me) of IFS varible in EXPANSION section, Word
> Splitting subsection. I'll quote it here:

> >  Word Splitting
> >      The shell scans the results of parameter expansion, command substitu-
> >      tion,  and  arithmetic  expansion  that  did  not occur within double
> >      quotes for word splitting."
> example:
> var="a  b c"
> echo $var

> >      The shell treats each character of IFS as a delimiter, and splits the
> >      results  of  the other expansions into words on these characters.  If
> >      IFS is unset, or its  value  is  exactly  <space><tab><newline>,   the
> >      default, then any sequence of IFS characters serves to delimit words.

> this is clear.

> >      If IFS has a value other than the  default,  then  sequences  of   the
> >      whitespace  characters space and tab are ignored at the beginning and
> >      end of the word, as long as the whitespace character is in the value

> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> >      of  IFS  (an IFS whitespace character).

> that means, *unless the witespace character is back in the value of IFS*

> >                                              Any character in IFS that is
> >      not IFS whitespace, along with any adjacent  IFS  whitespace charac-
> >      ters,  delimits  a field.

> I think, I got it:
> IFS=":;"
> var="a:;b;::c"
> echo $var   # a  b   c

> Now the part that I can barely understand:

> >                                A sequence of IFS whitespace characters is
> >      also treated as a delimiter.  If the value of IFS is  null,  no   word
> >      splitting occurs.

> >      Explicit  null  arguments ("" or '') are retained.  Unquoted implicit
> >      null arguments, resulting from the expansion of parameters that have
> >      no  values,  are  removed.   If a parameter with no value is expanded
> >      within double quotes, a null argument results and is retained.

> >      Note that if no expansion occurs, no splitting is performed.

> Could someone, please, expand this last part, add a few examples.

    Multiple adjacent whitespace characters are considered a single
    delimiter, but multiple non-whitespace characters each delimit a
    field.

    In your example, "a:;b;::c" contains 6 fields, including 3 empty
    fields. But "a:;b c" contains only 3 ("a", "", and "b c") or 4 if
    you add a space to IFS.

    You can see the splitting more clearly if you use printf instead
    of echo:

printf "%s\n" $var

--
    Chris F.A. Johnson                     <http://cfaj.freeshell.org>
    ==================================================================
    Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
    <http://www.torfree.net/~chris/books/cfaj/ssr.html>

 
 
 

section "Word Splitting" - IFS

Post by Eric Moor » Thu, 23 Jun 2005 16:08:53



> Hi!
> I am translating man 1 bash in my native languge. There is a somewhat
> confu- sing explanation (at leas for me) of IFS varible in EXPANSION
> section, Word Splitting subsection. I'll quote it here:

> >  Word Splitting
> > The shell scans the results of parameter expansion, command substitu-
> > tion,  and  arithmetic  expansion  that  did  not occur within double
> > quotes for word splitting."
> > The shell treats each character of IFS as a delimiter, and splits the
> > results  of  the other expansions into words on these characters. If
> > IFS is unset, or its  value  is  exactly  <space><tab><newline>, the
> > default, then any sequence of IFS characters serves to delimit words.

> this is clear.

> > If IFS has a value other than the  default,  then  sequences  of the
> > whitespace  characters space and tab are ignored at the beginning and
> > end of the word, as long as the whitespace character is in the value
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> > of  IFS  (an IFS whitespace character).

> that means, *unless the witespace character is back in the value of IFS*

No, that means that whitespace characters in IFS are treated differently
from printable characters. If a whitespace character occurs in the IFS,
solely, or in combination with other characters (:; in your example)
contiguous whitespace characters are collapsed into a single whitespace
as far as word-splitting is concerned. Sequences of non-whitespace
characters are not collapsed.

examples:

~>a="a  :       b:::c"

~>printf "\"%s\"\n" $a
"a"
":"
"b:::c"

~>IFS=":"

~>printf "\"%s\"\n" $a
"a  "
"       b"
""
""
"c"

~>IFS=":        "

~>printf "\"%s\"\n" $a
"a"
"b"
""
""
"c"

Quote:> Now the part that I can barely understand:
> > A sequence of IFS whitespace characters is also treated as a delimiter.  

A sequence of whitespace characters is collapsed into a single delimiter,
See previous examples.

Quote:> > If the value of IFS is  null, no word splitting occurs.

try it:

~>IFS=""

~>printf "\"%s\"\n" $a
"a  :       b:::c"

> > Explicit  null  arguments ("" or '') are retained.  Unquoted implicit


~>printf "\"%s\"\n" $a ""
"a"
":"
"b:::c"
""

> > null arguments, resulting from the expansion of parameters that have
> > no values, are removed.


~>unset d

~>printf "\"%s\"\n" $a $d
"a"
":"
"b:::c"

Quote:> > If a parameter with no value is expanded
> > within double quotes, a null argument results and is retained.

printf "\"%s\"\n" $a "$d"
"a"
":"
"b:::c"
""

> > Note that if no expansion occurs, no splitting is performed.


~>printf "\"$a\"\n"
"a  :       b:::c"

Quote:> Could someone, please, expand this last part, add a few examples.

I hope this is enough for you

Eric

 
 
 

section "Word Splitting" - IFS

Post by nabi » Sun, 26 Jun 2005 15:18:12


Thank you very much, Cris and Eric! I is clear now.

-nabis

 
 
 

1. problems piping from "split" to "mail" (split | mail)



: Hi !
:
: Can anybody give me a hint why the following doesnt work?
:

:
: Piping from uuencode to split works flawless (checked it with "tee"), but
: something doesnt work out in piping from split to mail. What happens is:
: the   uuencoded file gets split up correctly (lets assume its split into
: four pieces) and four mails are sent of to the correct address, but they
: are empty. Instead the four files are found on disk. Any ideas?

Yeah, you didn't read the man page for split:

 split(1)
split(1)

 NAME
      split - split a file into pieces

 SYNOPSIS
      split [-l line_count] [-a suffix_length] [file [name]]

      split [-b n[k|m]] [-a suffix_length] [file [name]]

      Obsolescent:
      split [-n] [file [name]]

 DESCRIPTION
      split reads file and writes it in pieces (default 1000 lines) onto a
      set of output files.  [...]

*sigh*

--Dave
--
http://armf18.dow.on.doe.ca:6700/~dbrown "On two occasions I have been asked
[by members of Parliament], 'Pray, Mr.  Babbage, if you put into the machine
wrong figures, will the right answers come out?' I am not able rightly to
apprehend the kind of confusion of ideas that could provoke such a question."

2. Using Unix tape from W95 or NT

3. Definition of "word" in zsh (e.g., "backward-kill-word")

4. lpfilter grief

5. GETSERVBYNAME()????????????????????"""""""""""""

6. apache 1.1.0 and clients hanging

7. """"""""My SoundBlast 16 pnp isn't up yet""""""""""""

8. Multiple disks

9. Why the "x" in the words "Unix", "Linux"...?

10. picking"Display" subsections in "Screen" sections (XF86Config)

11. section "screen", subsection "display"

12. how should "read" builtin split line into words?

13. Type "(", ")" and "{", "}" in X...