How do I Split a String on a Delimiter in Bash?

The string is the data type in the programming languages that is used for the manipulation of the text or ASCII characters. It can be displayed on the screen with different commands including the echo command or the printf command. 

In bash programming, the strings can be split into two or more than two parts using the delimiter. There are different methods of splitting the string on a delimiter in the bash programming such as using the IFS. 

All these methods of splitting the string on a delimiter in bash programming have been explained in this post with the following outline:

  1. What is the String in Bash Scripting
  2. What are the Delimiters in Bash Scripting
  3. What are the Methods to Split a String on a Delimiter in Bash
  4. Method 1: Using the Internal Field Separator (IFS)
  5. Method 2: Using the cut Command
  6. Method 3: Using the tr Command
  7. Method 4: Using the awk Command

Let’s dig out the methods in detail after understanding the string and a delimiter. 

What is the String in Bash Scripting?

In bash programming, the strings are sequences to handle the ASCII characters similar to the other programming languages. The strings can be used with the single quotes (‘ ‘), the double quotes (“ “), or with the quotes in the bash programming. 

If the strings are used with the variables, then the double quotes are necessary. For a better understanding, run the commands below in the bas terminal:

$ web='ItsLinuxFoss'

$ echo $web

$ echo -e "Welcome to $web"

In the above example, the string “ItsLinuxFoss” is stored in the variable “web” and then displayed with the “Welcome to ” string. 

What are the Delimiters in Bash Scripting?

The delimiters are the characters or the symbols that specify the beginning or the end of the text or the strings in the programming languages. They are an important part when the data is parsed because they break down the strings into small parts to extract information from them easily. 

The different examples of the delimiters are explained below:

  • Comma(,): These are used to separate the values in Comma Separated Files (CSV) such as (Mexico, Canada, Florida)
  • Colon(:): These are used to separate the values in the configuration files
  • Semicolon (;):  These are also used in the CSV files to distinguish the words or phrases

These are the common examples of the delimiters.

What are the Different Methods to Split a String on a Delimiter in Bash?

Splitting a string on a delimiter in bash programming is used for different purposes including data parsing and text processing. Different methods of splitting a string on a delimiter in bash programming are available, some of which are explained below in detail.

Method 1: Using the Internal Field Separator (IFS)

In this method, a variable known as the IFS variable is specified to recognize the delimiter. After recognizing the delimiter, the IFS variable splits the data after that delimiter. 

Let us create a new bash file by using the nano text editor:

$ nano MyFile.sh

Now write the bash script to split the string on a delimiter using the IFS approach. For example, store the names of the authors in the variable “names” by using the comma delimiter. Then define the IFS variable that splits the variable values on the comma delimiter. And finally display the split results on the computer screen using the for loop. 

The bash script for the above scenario will be as below:

#!/bin/bash

names="maddox,milton,henry"

IFS=',' read -ra name <<< "$names"

for element in "${name[[@]]}"; do
  echo "$element"
done

Save the bash script using the CTRL+S and run it with the following command:

$ bash MyFile.sh

The names have been split on the delimiter “comma” in bash programming successfully.

Method 2: Using the cut Command

Another method of splitting the string on a delimiter in bash programming is using the cut command. In the cut command, users are supposed to specify the delimiter with its “d” option and specify the column with the “f” option of the command. Store this value in the new variable and use the echo command to print the output on the screen. 

For example, in the bash file created with the MyName, change the bash script with the below-mentioned script:

#!/bin/bash
names="maddox,milton,henry"

first_name=$(echo "$names" | cut -d ',' -f 1)
second_name=$(echo "$names" | cut -d ',' -f 2)
third_name=$(echo "$names" | cut -d ',' -f 3)

echo "First author: $first_name"
echo "Second author: $second_name"echo "Third author: $third_name"

Again save the file and run the bash script with the below-mentioned command:

$ bash MyFile.sh

The “names” string has been split on a comma delimiter as can be seen on the screen. 

Method 3: Using the tr Command

The “\n” newline can also be used to split the string on a delimiter in bash programming. In this method, store the string with the delimiter in the variable. Then using the tr command, replace the delimiter with the \n notation and print the output on the screen. For example, replace the bash script of the MyFile with the mentioned script:

#!/usr/bin/env bash

names="maddox,milton,henry"

name=$(echo $names | tr "," "\n")

for addr in $name
do
    echo "$addr"
done

Run the bash script once it is saved with the CTRL+S:

$ bash MyFile.sh

The string has been split successfully on the delimiter comma as shown on the screen. 

Method 4: Using the awk Command

The last method of splitting the string on a delimiter in bash programming is using the awk command. This method is similar to “Method 2” explained in the above section. Specify the delimiter with the “F” option of the awk command and then use the numbers of fields with the $ sign to specify them on printing. For example, make the following changes in the bash script of “MyFile”:

#!/usr/bin/env bash

names="maddox,milton,henry"

echo "$names" | awk -F ',' '{print $1, $2, $3}'

Now run the script with the below-mentioned command after saving the file with the CTRL+S shortcut key:

$ bash MyFile.

These are the four different methods that can be used to split a string on a delimiter in bash programming. 

Conclusion

To split a string on a delimiter in bash programming, users can use the IFS methods, the cut command, the tr command, or the awk command. All the mentioned methods are explained with the examples in this post. The strings are split on a delimiter for several reasons such as data transformation. This blog develops a better understanding of the readers to split a string on a delimiter in bash programming.