From owner-freebsd-questions@FreeBSD.ORG Sun Aug 12 19:09:28 2007 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 13BC016A419 for ; Sun, 12 Aug 2007 19:09:28 +0000 (UTC) (envelope-from sonicy@otenet.gr) Received: from kane.otenet.gr (kane.otenet.gr [195.170.0.77]) by mx1.freebsd.org (Postfix) with ESMTP id 9030613C457 for ; Sun, 12 Aug 2007 19:09:27 +0000 (UTC) (envelope-from sonicy@otenet.gr) Received: from atlantis.dyndns.org (athedsl-305956.home.otenet.gr [85.73.242.194]) by kane.otenet.gr (8.13.8/8.13.8/Debian-3) with ESMTP id l7CJ9ORx012941; Sun, 12 Aug 2007 22:09:24 +0300 Message-ID: <46BF5AE4.2010206@otenet.gr> Date: Sun, 12 Aug 2007 22:09:24 +0300 From: Manolis Kiagias User-Agent: Thunderbird 2.0.0.5 (X11/20070719) MIME-Version: 1.0 To: Rakhesh Sasidharan References: <20070812195535.V86618@obelix.home.rakhesh.com> In-Reply-To: <20070812195535.V86618@obelix.home.rakhesh.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Cc: freebsd-questions@freebsd.org Subject: Re: Question on the IFS variable (not a FreeBSD question) X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 12 Aug 2007 19:09:28 -0000 Rakhesh Sasidharan wrote: > > Hi, > > This isn't really a FreeBSD question. But I figure most people on this > list would know the answer and so I'm asking. I've tried to get the > answer out of Google, but I guess I am not asking it the right > question and so not getting much hits. > > I understand that the default value of the IFS variable in bash is > "space, tab, newline". For a script I am playing around with, I want > to change IFS to be just newline. I tried the obvious like > > IFS="\n" > -or- > IFS='\n' > > but that doesn't seem to do the trick coz then the letter "n" ends up > being the separator. > > A bit of Google searching got me the solution too. That I must set IFS > this way: > > IFS=$'\n' > > I did that, and sure enough things work the way I want! > > So my question is this: how come things work when I set IFS to $'\n' > instead of just plain '\n'? I don't recollect seeing such a way of > setting variables before, and so I'm curious about it. > > TIA, > Rakhesh > _______________________________________________ > freebsd-questions@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-questions > To unsubscribe, send any mail to > "freebsd-questions-unsubscribe@freebsd.org" > The IFS=$'\n' is, as you found, correct for bash. See some details for this in the following post: http://osdir.com/ml/shells.bash.bugs/2004-10/msg00104.html Do a little experiment (inspired from the post stated above): #export IFS="\n" #printf '<%s>\n' "$IFS" | cat -vt will give <\n> ==> not what you expect #export IFS='\n' #printf '<%s>\n' "$IFS" | cat -vt will give <\n> ==> again, not what you expect #export IFS=$'\n' #printf '<%s>\n' "$IFS" | cat -vt will give < > definitely a new line character (finally...) I am not certain of the explanation, but from the above it seems to me the IFS does not evaluate special '\something' characters unless there is a $ in front. That is, of course, what you would do to get the value of a shell variable. It seems then these characters need to be evaluated in the same way.