Bash Split String by Delimiter

Written by: Emmah Moraa   |   Last updated: April 16, 2023

Splitting string data is essential in some specific tasks, such as reading log files line by line to get specific information like date. Most programming languages have a built-in function known as 'split' for dividing string data into various parts.

On the other hand, the bash does not contain such built-in functions. Therefore, it uses delimiters for splitting string data. Delimiters are often either a special character or a sequence of characters separating parts of a string.

 In this tutorial, we will learn various ways to split string data using different examples.

1. Using $IFS variable

The special variable $IFS is called Internal Field Separator. It is used in bash to split a string into words by assigning a specific delimiter. White space is the default delimiter for $IFS. However, other values such as '\n', '\t', '-' etc. can be used.

After assigning the delimiter, the string can be read by two options: '-r' and '-a'. The first option, '-r', reads the backslash (\) as a character rather than escape character while the '-a' option stores the split-ted words into an array variable.

The examples below show different ways to split string data with $IFS.

Example 1: Bash Split String by Space

#!/bin/bash
# Define the string value
my_string="Welcome to Linux OS"
# Setting the space delimiter
IFS=' '
# Read the split words into an array using the set delimiter (space)
read -a strarr <<< "$my_string"
# use for loop to print each value of the array
for word in "${strarr[@]}";
do
  echo "$word"
done

The script defines the string value to split and specifies the delimiter as space. The set delimiter is then used to split the words into an array which are then printed out using a for loop.

bash script to output - split string by space

The output prints each of the split words of the provided string: “Welcome to Linux OS”. It shows that string splitting is based on the space delimiter.

Example 2: Bash Split String by a Particular Character

#!/bin/bash
# Reading the string value provided
echo "Input your First Name, Last Name and State separated by a comma: "
read input
# set the comma as a delimiter
IFS=','

# read the string as an array
read -a strarr <<< "$input"

# Print the splitted words
echo "First Name : ${strarr[0]}"
echo "Last Name: ${strarr[1]}"
echo "State: ${strarr[2]}"

The script prompts the user to key in specific input, reads the input, and sets the comma as a delimiter. It then reads the input string as an array (starr).

bash script to output - split string by a particular character

From the output, the user provided “John, Doe, Ohio” as their input. This string was then splitted based on the comma delimiter and the output printed.

2. Using read command

A string can also be divided into sections without using the $IFS variable. For instance, the readarray command used with -d option can be used to split string data. Just like in $IFS, the -d command defines the separator character.

#!/bin/bash
# Read the string value provided
echo "Enter text with a colon (:) to split"
read input
# Split the provided text based on the colon delimiter
readarray -d : -t strarr <<< "$input"
# Use for loop to print each value of the array
for (( i=0; i< ${#strarr[*]}; i++))
do
   echo "${strarr[i]}"
done

This script prompts the user to enter input with a colon. It reads the string provided into an array and splits it based on the colon delimiter. The for loop is then used to print each value of the array.

splits it based on the colon delimiter

In this example, the string “Python:Node:React” is taken in as input for splitting. The readarray command used with the -d option splits the provided string data based on the colon (:) delimiter.

The read command reads raw input (option -r), it interprets backslashes literally rather than treating them as escape characters. The option -a with read command stores the word read into an array in bash. The bash loop prints the string in split form.

#!/bin/bash
# Define a string value
my_string="Windows;Linux OS; Debian; Fedora"
# Set the delimiter
IFS=';'
# Split the string value provided
read -ra strarr <<< "$my_string"
# Use for loop to print the splitted string
for i in "${strarr[@]}"
do
   echo $i
done

This script defines the string to split and sets the semicolon as the delimiter. The for loop is then used to read the value of the splitted string.

semicolon as the delimiter

The output shows the results printed after splitting the string value, “Windows;Linux OS; Debian; Fedora”,  based on the semicolon delimiter.

3. Using tr command

Instead of the read command, the tr command is used to split the string on the delimiter. The following example shows how this command is used.

#!/bin/bash
# Define the string value to split
my_string="Windows; Linux OS; Debian; Fedora"
strarr=($(echo $my_string  | tr ";" "\n"))
# Use for loop to print the split string
for n in "${strarr[@]}"
do
   echo "$n"
done

This example is pretty much the same as the previous one but instead of the read command the script uses the tr command.

using tr command

In this approach, array elements are divided based on semicolon and space delimiters. The string “Windows; Linux OS; Debian; Fedora '' results in the output shown above. Some elements are treated as separate words. For example, ‘Linux OS’ is treated as two words: Linux and OS. 

4. Using awk command

The awk Linux command works in bash and shell distributions. It returns the exit code with the result. The pipe (|) symbol is used to pass input to the awk command. Awk provides a split function to create an array based on the given delimiter. 

The syntax for the split function is:

split(SOURCE, DESTINATION,DELIMITER)

However, the delimiter is optional and defaults to space if not provided.

Example:

#!/bin/bash
# Define a string value
my_string="9 10 11"
# split the string provided.
echo $my_string | awk '{split($0,b);print b[1]; print b[2]; print b[3]}'

In this script, the delimiter is not provided. Therefore, the string value defined is split based on the space delimiter.

split based on the space delimiter using awk

The defined string value was “9 10 11”. This string is split based on the space delimiter and the output is printed as shown above.

5. Using cut command

The cut command can be used to split a string on a delimiter.  The -d option is used to specify the delimiter. The -f option is used to specify the string to split.

#!/bin/bash
# Define the string value
my_string="Python;React;Angular"
# Split the string provided on the semicolon (;) delimiter
echo $my_string | cut -d ';' -f 1

The script shows how the cut command is used to split a string on a delimiter. The defined string value is splitted based on the semicolon delimiter. 

using cut command split based on the semicolon delimiter

In this example, the cut command is used to split the string “Python;React;Angular” based on the semicolon delimiter. The -d option specifies the delimiter which in this case is the semicolon(;). The  -f option specifies that the first element (Python) should be extracted.

6. Using sed command

The sed command can also be used to split string data. The syntax for this command is:

sed ‘s/delimiter/\n/g’
#!/bin/bash
# Define a string value
my_string="Hello:World"
# Split the string provided
sed 's/:/\n/g' <<< $my_string

In this case, g stands for global. It means that the substitution has to be globally, that is, for any occurrence. The example above shows how sed command is used to split string data using colon as a delimiter.

split string data using colon as a delimiter using sed

The output shows that the defined string value, “Hello:World”,  was split into two words: Hello and World, based on the colon delimiter.

Conclusion

Splitting strings is useful in some specific use cases. Unlike other programming languages, bash splits strings using delimiters with commands such as IFS variable, tr, cut, awk, and sed commands.

About The Author

Emmah Moraa

Emmah Moraa

A software engineer proficient in scripting languages such as Powershell, Bash, and Python. I have solid experience in Linux (Ubuntu, CentOS) administration. I also have experience working with AWS – including but not limited to EC2, S3, RDS, IAM, and Route53.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment