From owner-freebsd-questions@FreeBSD.ORG Mon Aug 25 14:10:55 2008 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 E193C1065689 for ; Mon, 25 Aug 2008 14:10:55 +0000 (UTC) (envelope-from unga888@yahoo.com) Received: from web57001.mail.re3.yahoo.com (web57001.mail.re3.yahoo.com [66.196.97.105]) by mx1.freebsd.org (Postfix) with SMTP id 782698FC16 for ; Mon, 25 Aug 2008 14:10:55 +0000 (UTC) (envelope-from unga888@yahoo.com) Received: (qmail 36546 invoked by uid 60001); 25 Aug 2008 14:10:54 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=X-YMail-OSG:Received:X-Mailer:Date:From:Reply-To:Subject:To:Cc:In-Reply-To:MIME-Version:Content-Type:Message-ID; b=e5ZxT31EP2l+ULreA68Ewbr613LTC5XCAtsKrrQruYKSe6j5Zb13Z3P/THRvkIrohUsRTcT6kynE0JUke1i87QqlieZXeEpMuhpevd98e/x6y/OH6cIRSKn4Pgro8g7qTe2TZJclHMrexQYjAAk1UI4aJz8mWUBKFeFrj5gvzhw=; X-YMail-OSG: RqPn0KgVM1m43nn3YNaC3w8aJZ.c72IUWIWznkUAxm5V0rqhnZl9yCBmGSkpKKvQ8Lj705GmP72FDHLw8OhuywKx.HdPrEe20SCxw.U7FzNXUctqxPU3CJ3Ij74mK1UEzFUaPpJ3n01UniSQG.h17MzDc5r7r0MQsyJg5_0X.Kw- Received: from [165.21.155.13] by web57001.mail.re3.yahoo.com via HTTP; Mon, 25 Aug 2008 07:10:54 PDT X-Mailer: YahooMailWebService/0.7.218.2 Date: Mon, 25 Aug 2008 07:10:54 -0700 (PDT) From: Unga To: Barry Byrne In-Reply-To: <01c301c906a9$57f7d830$72010c0a@wbt.wbtsystems.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Message-ID: <272704.36116.qm@web57001.mail.re3.yahoo.com> Cc: freebsd-questions@freebsd.org Subject: RE: string split, bash and IFS X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: unga888@yahoo.com List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 25 Aug 2008 14:10:56 -0000 --- On Mon, 8/25/08, Barry Byrne wrote: > From: Barry Byrne > Subject: RE: string split, bash and IFS > To: unga888@yahoo.com, freebsd-questions@freebsd.org > Date: Monday, August 25, 2008, 7:54 PM > > -----Original Message----- > > From: owner-freebsd-questions@freebsd.org > > [mailto:owner-freebsd-questions@freebsd.org] On Behalf > Of Unga > > Sent: 25 August 2008 10:40 > > To: freebsd-questions@freebsd.org > > Subject: string split, bash and IFS > > > How to use bash and IFS to split a string? > > > > eg. > > $string = "Name:Surname:10" > > IFS=: > > echo "$string" | read name surname age > > > > This does not work for some reason. The read does not > create > > name, surname and age variables. Any idea why? > > > > Appreciate your reply. > d-questions-unsubscribe@freebsd.org" > > Unga: > > I think your problem is that each element of the pipeline > runs in a separate > process, so has no access to the variables from other > processes. You could > try something like: > > echo "Name:Surname:10" | ( IFS=: ; read name > surname age ; echo $surname) > Thanks for the reply. Your statement prints the Surname but the variable $surname disappear after that and not available for further processing. I'm referring to two sources for this: 1. Learning the bash shell, 2nd Edition. O'Reilly publishers Where on page 170, under read section The basic syntax is: read var1 var2... This statement takes a line from the standard input and breaks it down into words delimited by any of the characters in the value of the environment variable IFS. The words are assigned to variables var1, var2, etc. 2. http://www.unix.com/shell-programming-scripting/29202-perl-like-split-function-bash.html It looks like it works for others but here on FreeBSD 7.0, bash 3.2.33(0)-release, I find it difficult to get this syntax to work. > Also, probably a typo - but you're assignment of string > in the first line > should omit the $ sign. > Yes, sure is a typo. Regards Unga