string split, bash and IFS
Jim Hertzler
jimhertzler at ntelos.net
Fri Sep 5 16:38:30 UTC 2008
#!/bin/bash
# Split the command line argument on the colon character.
SaveIFS=$IFS
IFS=":"
declare -a Array=($*)
IFS=SaveIFS
echo "Array[0]=${Array[0]}"
echo "Array[1]=${Array[1]}"
echo "Array[2]=${Array[2]}"
echo "Array[3]=${Array[3]}"
Unga wrote:
>
> Hi all
>
> 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.
>
> Kind regards
> Unga
>
>
>
> _______________________________________________
> freebsd-questions at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to
> "freebsd-questions-unsubscribe at freebsd.org"
>
>
--
View this message in context: http://www.nabble.com/string-split%2C-bash-and-IFS-tp19140697p19335104.html
Sent from the freebsd-questions mailing list archive at Nabble.com.
More information about the freebsd-questions
mailing list