Here's how I would apply this problem: use the IFS variable to tell the shell (bash) that you want to split the string into colon tokens.
$ cat split.sh #!/bin/sh # Script to split fields into tokens # Here is the string where tokens separated by colons s="first column:second column:third column" IFS=":" # Set the field separator set $s # Breaks the string into $1, $2, ... i=0 for item # A for loop by default loop through $1, $2, ... do echo "Element $i: $item" ((i++)) done
Run it:
$ ./split.sh Element 0: first column Element 1: second column Element 2: third column
Hai vu
source share