string split, bash and IFS

Barry Byrne barry.byrne at wbtsystems.com
Mon Aug 25 11:54:34 UTC 2008


> -----Original Message-----
> From: owner-freebsd-questions at freebsd.org 
> [mailto:owner-freebsd-questions at freebsd.org] On Behalf Of Unga
> Sent: 25 August 2008 10:40
> To: freebsd-questions at 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 at 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)

Also, probably a typo - but you're assignment of string in the first line
should omit the $ sign.

 - barry



More information about the freebsd-questions mailing list