Chris F.A. Johnson
Unix shell programming notes

4 May 2004

Extracting parts of a string

A frequent requirement in shell scripts is to split a string into two or more components. Even in a Bourne shell, external commands are not needed.

Far too often, I see scripts like this:

string="123,456,789"
v1=`echo $string | cut -d, -f1`
v2=`echo $string | cut -d, -f2`
v3=`echo $string | cut -d, -f3`

It uses three calls to an external command (cut) where none is necessary.

External commands are rarely needed to parse a string in a POSIX shell, and even using a Bourne shell they can often be avoided.

The shell splits strings into words using the value of the Internal Field Separator (IFS) variable as the delimiter, so the same thing can be accomplished in two ways.

The first method uses the positional parameters:

string="123,456,789"
oldIFS=$IFS
IFS=,
set -- $string
v1=$1
v2=$2
v3=$3
IFS=$oldIFS

The second reads the values from a here document:

string="123,456,789"
IFS=, read v1 v2 v3 <<EOF
 $string
EOF

In bash and ksh, a here string may be used instead:

string="123,456,789"
IFS=, read v1 v2 v3 <<< "$string"

The third method uses POSIX parameter expansion.

Modified 17 Nov 2011