Open In App

Bash Scripting – Split String

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to split strings in a bash script.

Dividing a single string into multiple strings is called string splitting. Many programming languages have a built-in function to perform string splitting but there is no built-in function in bash to do so. There are various methods to perform split string in bash. Let’s see all methods one by one with examples.

Method 1: Using IFS variable

$IFS(Internal Field Separator) is a special shell variable. It is used to assign the delimiter (a sequence of one or more characters based on which we want to split the string). Any value or character like ‘\t’, ‘\n’, ‘-‘ etc. can be the delimiter. After assigning the value to the $IFS variable, the string value needs to be read. We can read string using ‘-r’ and ‘-a’ options. 

  • ‘-r’: It read backslash (\) as a character instead of an escape character 
  • ‘-a’: It is used to store the split-ted words into an array variable that is declared after the -a option. 

Example 1: Split the string by space 

Code:

#!/bin/bash

# String
text="Welcome to GeeksforGeeks"

# Set space as the delimiter
IFS=' '

# Read the split words into an array
# based on space delimiter
read -ra newarr <<< "$text"


# Print each value of the array by using
# the loop
for val in "${newarr[@]}";
do
 echo "$val"
done

Output:

Welcome
to
GeeksforGeeks

Example 2: Split string by a symbol

String split using @ symbol.

Code:

#!/bin/bash

#String
text="Welcome@to@GeeksforGeeks@!!"

# Set @ as the delimiter
IFS='@'

# Read the split words into an array 
# based on space delimiter
read -ra newarr <<< "$text"


# Print each value of the array by 
# using the loop
for val in "${newarr[@]}";
do
 echo "$val"
done

Output:

Welcome
to
GeeksforGeeks
!!

Method 2: Without using IFS variable

In this method, readarray command with the -d option is used to split the string data.  â€˜-d’: this option act as an IFS variable to define the delimiter.

Example 1: Split string by space

Code:

#!/bin/bash

# Read the main string
text="Welcome to GeeksforGeeks"

# Split the string by space
readarray -d " " -t strarr <<< "$text"

# Print each value of the array by 
# using loop
for (( n=0; n < ${#strarr[*]}; n++))
do
 echo "${strarr[n]}"
done

Now, let’s see how the script behaves:

  • The script sets the value of text to “Welcome to GeeksforGeeks.”
  • The readarray command splits the string into an array using space as the delimiter, resulting in the array strarr containing the following elements: “Welcome”, “to”, “GeeksforGeeks.”
  • The for loop iterates over each element of the strarr array.
  • It prints each element one by one on separate lines.

Output:

Welcome
to
GeeksforGeeks

Example 2: Split using a colon as a delimiter

Code:

#!/bin/bash

# Read the main string
text="Welcome:to:GeeksforGeeks"

# Split the string based on the delimiter, ':'
readarray -d : -t strarr <<< "$text"

# Print each value of the array by using
# loop
for (( n=0; n < ${#strarr[*]}; n++))
do
 echo "${strarr[n]}"
done

Output:

Welcome
to
GeeksforGeeks

Method 3: Split the string with a multi-character  delimiter

In this method, a variable is used to store string data and another variable is used to store multi-character delimiter data. An array variable is also declared to store the split-ted string.

Code:

# Define the string to split
text="HelloRomy HelloPushkar HelloNikhil HelloRinkle"

# store multi-character delimiter 
delimiter="Hello"

# Concatenate the delimiter with the 
# main string
string=$text$delimiter

# Split the text based on the delimiter
newarray=()
while [[ $string ]]; do
 newarray+=( "${string%%"$delimiter"*}" )
 string=${string#*"$delimiter"}
done

# Print the words after the split
for value in ${newarray[@]}
do
 echo "$value "
done

Output:

Romy  
Pushkar  
Nikhil  
Rinkle 

Last Updated : 15 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads