Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

why is echo different?

9 views
Skip to first unread message

Parmenides

unread,
Dec 4, 2009, 6:31:21 AM12/4/09
to
IFS=:
echo $IFS # the result is space
echo "$IFS" # the result is :
Is there any difference between $IFS and "$IFS"

Andrew McDermott

unread,
Dec 4, 2009, 7:09:08 AM12/4/09
to
Parmenides wrote:

> IFS=:
> echo $IFS # the result is space

a space, or just a blank line?

> echo "$IFS" # the result is :
> Is there any difference between $IFS and "$IFS"

Also
$ aaa="a
> b
> c
> d
> e"
$ echo $aaa
a b c d e
$ echo "$aaa"
a
b
c
d
e

IFS is the internal field separator. When you quote a string with double
quotes it is a single token. When you don't quote a string it is broken
into tokens using IFS to identify field separators. IFS normally contains
the space, tab and newline characters, so in my example 'echo $aaaa'
becomes 'echo "a" "b" "c" "d" "e"' (ie five arguments) while 'echo "$aaa"'
contains a single argument - a string on five lines. In your example $IFS
is either being interpreted as spaces (if indeed it is printing a space),
or as a string which contains a ':'.

echo separates its arguments with a space when it prints them.

Andrew


Stephane Chazelas

unread,
Dec 4, 2009, 7:59:56 AM12/4/09
to
On 2009-12-04, Andrew McDermott <a.p.mc...@NOSPAM-rl.ac.uk> wrote:
> Parmenides wrote:
>
>> IFS=:
>> echo $IFS # the result is space
>
> a space, or just a blank line?
[...]

It will depend on the shell. For some shells, IFS is a field
separator (zsh, pdksh, ash), and for some, it's a field
terminator (bash, AT&T ksh). POSIX is not very clear what it
should be (or at least wasn't the last time I checked).

So where IFS is a separator, $IFS above is split into "" and ""
which echo outputs separated by a space. And where it's a
terminator, into just one "".

That's something to bear in mind when for instance using IFS=:
to split $PATH like variables.

--
Stephane

Geoff Clare

unread,
Dec 4, 2009, 9:04:58 AM12/4/09
to
Stephane Chazelas wrote:

> It will depend on the shell. For some shells, IFS is a field
> separator (zsh, pdksh, ash), and for some, it's a field
> terminator (bash, AT&T ksh). POSIX is not very clear what it
> should be (or at least wasn't the last time I checked).

It was clarified in the 2008 edition. (It says the characters
in IFS are used as field terminators.)

--
Geoff Clare <net...@gclare.org.uk>


Parmenides

unread,
Dec 4, 2009, 9:40:19 AM12/4/09
to
On 12月4日, 下午8时09分, Andrew McDermott <a.p.mcderm...@NOSPAM-rl.ac.uk>
wrote:

I see, it means that shell interpretes the content of IFS in terms of
its content(:), and the result is null.
thanks a lot.

Parmenides

unread,
Dec 4, 2009, 9:40:32 AM12/4/09
to
On 12月4日, 下午8时09分, Andrew McDermott <a.p.mcderm...@NOSPAM-rl.ac.uk>
wrote:

I see, it means that shell interpretes the content of IFS in terms of

Seebs

unread,
Dec 4, 2009, 1:33:58 PM12/4/09
to

Well, without quotes, the expansion of $IFS is subject to field splitting,
meaning that any characters in $IFS can be replaced by nulls which implicitly
separate fields.

So when you echo $IFS without quotes, it expands to a colon, and is then
separated out into fields, where a field is anything other than colons,
separated by any colons. Which means that it ends up being no arguments
at all. So you're doing echo with no argument.

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

Sven Mascheck

unread,
Jan 2, 2010, 1:09:59 PM1/2/10
to
Geoff Clare wrote:
> Stephane Chazelas wrote:
>

>> For some shells, IFS is a field separator (zsh, pdksh, ash),
>> and for some, it's a field terminator (bash, AT&T ksh).

>> POSIX [...]

> [...] says the characters in IFS are used as field terminators.

And adding to Stephane's results:
- field separator in earlier posh and earlier ash
- field terminator since posh-0.6.15, since early
(d)ash-0.4.x, ~NetBSD3.1, and in mksh

There are even more variations, probably only important to notice
for such experiments: if the variable contains only characters from
IFS, then one field collapses in mksh and recent posh, or all fields
collapse in Bourne shell and bash-1.05. But such corner cases in
shell tend to be a house of cards, anyway.

0 new messages