Bash Split String into Array: [4 Easy Method]

Introduction

In this tutorial, we will learn about bash split string into array using 4 easy method”. In Linux, we can split an input string into an array in bash by using various techniques. This becomes useful when we have a string with elements separated by a delimiter (e.g comma, space, semi-colon or any other character) and we want to store these elements in an array for further processing. We will discuss four useful way to store the string elements in array in the upcoming sections of this tutorial.

 

Bash Split String into Array: [4 Easy Method]

Array in Linux Overview 

In Linux, array is a collection of values or elements that are stored together under a single variable name. We often use array in Shell scripting, specially in Bash to store and manipulate lists of data. To create an array in Bash, we use below syntax

Array Syntax:

array_name=(val1 val2 val3 ... valN)

 

To access elements of array in Bash, we use below syntax

${array_name[index]}

 

To find the length of array (total number of elements in the array) in Bash, we use below syntax

${#array_name[@]}

 

Bash Split String into Array: [4 Easy Method]

We will now look at the four possible and useful ways to split the input string and store it in array in Bash. Input string can be passed as user input to the Bash script or we can pass the input string within the script. For the ease of this tutorial, we will use common script for all four solution and make the changes to the same script. This will help us understand how different ways to store input string in array gives the same result.

Also read: [Solved]: chown “Operation not permitted” error

Method-1: Using Parenthesis ()

In this method, we will split the input string by storing it in an array. After we store the input string in array, each substring becomes individual elements of an array.  To demonstrate the concept, create and save below code in a file as shown below.

[root@linuxnasa~]# vi split_string.sh
#!/bin/bash

var="Alice Bob Marry"

#Store var into an array
vararray=($var)

#Print the string from array
echo "Strings in Array: ${vararray[@]}"
echo -e "Total number of strings in Array: ${#vararray[@]}\n"

 

OUTPUT

[root@linuxnasa ~]# sh split_string.sh
Strings in Array: Alice Bob Marry
Total number of strings in Array: 3

In the above example, after storing the input string in array, each substring became elements of array. Hence, when we check the size of array, it will return 3 as there are 3 sub-strings present in the input string.

 

Method-2: Using read Keyword

In this method, we will  store the input string into array  using read -a method. The default delimiter in read method is white space. Create and save below code in a file.

[root@linuxnasa~]# vi split_string.sh
#!/bin/bash

var="Alice Bob Marry"

#Store var into an array
read -a vararray <<< $var

#Print the string from array
echo "Strings in Array: ${vararray[@]}"
echo -e "Total number of strings in Array: ${#vararray[@]}\n"

 

OUTPUT

[root@linuxnasa ~]# sh split_string.sh
Strings in Array: Alice Bob Marry
Total number of strings in Array: 3

In the above example, we have stored the input string into an array using read -a method. When we retrieve the array elements and size of the array, we get the same output as we had seen in Method-1

 

Method-3: Using Delimiter

In this method, we will use delimiter to split the input string. A delimiter is defined by combining read keyword with IFS (Internal Field Separator). Let see the below code. Create and save the below code in a file.

[root@linuxnasa~]# vi split_string.sh
#!/bin/bash

var="Alice;Bob;Marry"

#Store var into an array
IFS=";" read -a vararray <<< $var

#Print the string from array
echo "Strings in Array: ${vararray[@]}"
echo -e "Total number of strings in Array: ${#vararray[@]}\n"

 

OUTPUT

[root@linuxnasa~]# sh split_string.sh
Strings in Array: Alice Bob Marry
Total number of strings in Array: 3

In the above example, we have taken input string where each sub-string is separated by semi-colon (;) as delimiter. We store the input string in an array using the IFS where delimiter is semi-colon. When we retrieve the array elements and size of the array, we get the similar output as we had seen in previous two methods.

 

Method-4: Using tr Keyword

In this method, we will use tr command to store process the input string and store it in an array. tr is multi purpose tool. We will use it to convert semi-colon separated input string into white space separated string. We will then store this string in an array for further processing. Create below code and save it in a file.

[root@linuxnasa~]# vi split_string.sh
#!/bin/bash

var="Alice;Bob;Marry"

#Change semi-colon to whitespace
vararray=(`echo $var | tr ';' ' '`)

#Print the string from array
echo "Strings in Array: ${vararray[@]}"
echo -e "Total number of strings in Array: ${#vararray[@]}\n"

 

OUTPUT

[root@linuxnasa~]# sh split_string.sh
Strings in Array: Alice Bob Marry
Total number of strings in Array: 3

In the above example, we have taken an input string separated by semi-colon (you can use any other character as delimiter). We have replaced the semi-colon with white space using tr method. After conversion, we have stored the input string into an array. When we retrieve the elements of array and size of array, we get the same output as we have seen in previous methods.

 

Summary

We have successfully stored the input string into array using various method. You can read more about tr command from tr man page.

Leave a Comment