Unix Shell - IFS

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > June 2006 > IFS





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author IFS
FrenKy

2006-06-08, 1:25 pm

Hi *,

while IFS= read -r i <&3; do
rm -- "$i"
done 3< list.txt

I've seen this example in several topics so far (IFS= read -r item)

What is the difference between
IFS= read -r i
and
read -r i

I know that IFS is field separator (and used to split the line to words
with read command), but I'm not sure what the IFS= read.... actually does?
Stephane Chazelas

2006-06-08, 1:25 pm

On Thu, 08 Jun 2006 17:19:21 +0200, FrenKy wrote:
> Hi *,
>
> while IFS= read -r i <&3; do
> rm -- "$i"
> done 3< list.txt
>
> I've seen this example in several topics so far (IFS= read -r item)
>
> What is the difference between
> IFS= read -r i
> and
> read -r i
>
> I know that IFS is field separator (and used to split the line to words
> with read command), but I'm not sure what the IFS= read.... actually does?


There are several things involved in the processing done by read
upon a line it reads.

IFS= read -r var

does no extra processing, it's a raw read.

read var1 var2

does more:
1- stripping of leading and trailing IFS white space characters
(the white space characters that are containes in IFS) from the
line
2- treat backslash as an escape character for line continuation
and for the separators.
3- splitting the line into words and store them in the variable
(using a complex algorithm)
4- remove the unescaped backslashes.

in IFS= read -r o
-r disables 2 and 4.
as there's only one variable, there's no splitting.

By removing all the white space characters from IFS (here setting
it to nothing, but we could have set it to anything not
containing any blanks like IFS=non-blank read -r i), we disable
1.

--
Stephane
FrenKy

2006-06-08, 7:23 pm

Stephane Chazelas wrote:
> On Thu, 08 Jun 2006 17:19:21 +0200, FrenKy wrote:
>
> There are several things involved in the processing done by read
> upon a line it reads.
>
> IFS= read -r var
>
> does no extra processing, it's a raw read.
>
> read var1 var2
>
> does more:
> 1- stripping of leading and trailing IFS white space characters
> (the white space characters that are containes in IFS) from the
> line
> 2- treat backslash as an escape character for line continuation
> and for the separators.
> 3- splitting the line into words and store them in the variable
> (using a complex algorithm)
> 4- remove the unescaped backslashes.
>
> in IFS= read -r o
> -r disables 2 and 4.
> as there's only one variable, there's no splitting.
>
> By removing all the white space characters from IFS (here setting
> it to nothing, but we could have set it to anything not
> containing any blanks like IFS=non-blank read -r i), we disable
> 1.
>

Thx.
Clear.
That is exactly what the man page says... I just had to red it again,
with your nice explanation.

Br,
FrenKy
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com