From: Parmenides on

The output is 4. While IFS='\n', the output is 1. It is obvious that
`var' is expanded as four string in the first case, while it is
expanded as a single string in the second case. The point is that IFS
has different meaning in the two cases. Is there any explanation?

#!/bin/bash

var='a
b
c
d'
IFS=$'\n'
count=0
for ch in $var
do
let count=count+1
done
echo $count

From: Bill Marcum on
On 2009-12-05, Parmenides <mobile.parmenides(a)gmail.com> wrote:
>
> The output is 4. While IFS='\n', the output is 1. It is obvious that
> `var' is expanded as four string in the first case, while it is
> expanded as a single string in the second case. The point is that IFS
> has different meaning in the two cases. Is there any explanation?
>
man bash:
Words of the form $'string' are treated specially. The word expands to
string, with backslash-escaped characters replaced as specified by the
ANSI C standard. Backslash escape sequences, if present, are decoded
as follows:
\a alert (bell)
\b backspace
...
From: pk on
Parmenides wrote:

>
> The output is 4. While IFS='\n', the output is 1.

IFS=$'\n'

sets IFS to a literal newline

IFS='\n'

sets IFS to either \ or n

From: Seebs on
On 2009-12-05, Parmenides <mobile.parmenides(a)gmail.com> wrote:
> The output is 4. While IFS='\n', the output is 1. It is obvious that
> `var' is expanded as four string in the first case, while it is
> expanded as a single string in the second case. The point is that IFS
> has different meaning in the two cases. Is there any explanation?

Yes. Mabye you should have checked the man page for bash?

> IFS=$'\n'

This is a bashism for supporting escape sequences. In standard shell,
write

IFS='
'

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet-nospam(a)seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 | 
Pages: 1
Prev: special variable $@ and $*
Next: 09-uggmax