[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: The IFS variable - a confusing issue.

From: Stephane Chazelas
Subject: Re: The IFS variable - a confusing issue.
Date: Fri, 8 Oct 2004 08:21:25 +0100
User-agent: Mutt/1.5.6i

On Thu, Oct 07, 2004 at 04:03:16PM -0400, Paul Jarc wrote:
> You could also save the original IFS, set it to ":", split $PATH, and
> then restore the original value:
> old_ifs=$IFS
> IFS=:
> set -f # disable globbing, in case $PATH contains any odd characters
> set -- $PATH
> set +f
> IFS=$old_ifs

Note that if IFS was previously unset, it's now empty which
leads to a completely different behavior.

A way to workaround it is:

${IFS+":"} unset old_ifs
${old_ifs+":"} unset IFS

> Side note: using the shell's word splitting means you won't notice
> empty elements as in "PATH=/foo::/bar".  So it won't exactly match the
> behavior of the shell's/exec*p's $PATH search for programs, which
> treats the empty element like ".".

Not really. Empty elements are not squeezed (except the last one
in some shells like bash) because ":" is not an "IFS white space
character" (the splitting behavior is different for normal
IFS characters and "white space" IFS characters (and more
complicated, when both type of characters are mixed in IFS)).

But I agree that as a trailing empty element is discarded, PATH 
splitting can't be handled (at least portably) with shell word

> > But just to make sure that the modified assignment did what I intended.. 
> > I had also added an "echo $IFS" immediately after and this did not give 
> > me the expected result.
> "echo $IFS" should always give an empty line, because the characters in
> $IFS are treated as whitespace for word splitting.

Well, it may return some space characters (echo may get a list
of empty arguments)

bash-2.03$ IFS=abcd
bash-2.03$ echo $IFS | cat -vte

(only 3 spaced because echo gets 4 arguments (the one after "d"
was discarded <1>a<2>b<3>c<4>d).

> Do this instead:
> echo "$IFS"

Or better:

printf '<%s>\n' "$IFS" | cat -vt

in case the first parameter of IFS is a "-" or the xpg_echo
option is on.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]