Bash split string means breaking one string into smaller fields or tokens by using a delimiter such as a space, comma, hyphen, colon, or another string. In Bash, the usual approach for a single-character delimiter is IFS with read -r -a. For a multi-character delimiter, use parameter expansion or another explicit parsing method because IFS treats each character as a possible separator, not as one complete delimiter string.

This tutorial shows how to split a string in Bash shell scripting, store the result in an array, print each token, get a specific field, and handle both single-character and multiple-character delimiters.

Bash split string methods at a glance

RequirementRecommended Bash approachImportant note
Split by space, comma, colon, hyphen, or another single characterIFS with read -r -aWorks well when one line must be split into an array.
Split and loop through every tokenUse for item in "${array[@]}"Quote the array expansion to keep each token intact.
Get the Nth field after splittingSplit into an array and use a zero-based indexThe first field is index 0, the second is index 1.
Split by a delimiter such as ABC, ::, or --split--Use Bash parameter expansion or a loopIFS is not suitable for a delimiter that must be matched as a full string.

Bash split string with IFS and read into an array

Split String with single character delimiter(s) in Bash using IFS

To split a string in bash using IFS, follow the below steps:

Step 1: Set IFS to the delimiter you would want. IFS='<delimiter>'
IFS is an internal variable that determines how Bash recognizes word boundaries. The default value of IFS is white space. If you set it to some other value, reset it to default whitespace.

Step 2: Read your string to a variable with options -ra.
read -ra ARR <<< "$str"

OptionDescription
-rBackslash does not act as an escape character.
-a ARRThe words(separated by IFS) are assigned to the sequential index of array ARR beginning at zero.

The common syntax is shown below. Use this pattern when the delimiter is one character, such as a comma or colon.

</>
Copy
IFS='DELIMITER' read -r -a array_name <<< "$string_variable"

Now you have your string split by the delimiter (set in IFS) stored in array ARR. ARR is just array name. Any string literal that is valid could be used as an array name.

Step 3: You may now access the tokens split into an array using a bash for loop.

When looping through the split array, use "${ARR[@]}" instead of ${ARR[@]}. The quoted form keeps each array element as a separate value even when a token contains spaces or special characters.

Its time for some examples. In the following two examples, we will go through example bash scripts where we use IFS to split a string.

Example 1: Bash Split String by Space

In this example, we split a string using space as a character delimiter. With whitespace splitting, Bash ignores extra leading and trailing whitespace and treats a run of whitespace as a separator between words.

Bash Script File

</>
Copy
#!/bin/bash

str="Learn to Split a String in Bash Scripting"

IFS=' '		# space is set as delimiter
read -ra ADDR <<< "$str"	# str is read into an array as tokens separated by IFS
for i in "${ADDR[@]}"; do	# access each element of array
    echo "$i"
done

Run the above bash shell script in a terminal.

Output

$ ./bash-split-string-example 
Learn
to
Split
a
String
in
Bash
Scripting

Example 2: Bash Split String by Symbol

Sometimes we may need to split a string by a delimiter other than space. To split a string in bash shell by a symbol or any other character, set the symbol or specific character to IFS and read the string to a variable with the options -ra mentioned in the below example.

Bash Script File

</>
Copy
#!/bin/bash

str="Learn-to-Split-a-String-in-Bash-Scripting"

IFS='-'		# hyphen (-) is set as delimiter
read -ra ADDR <<< "$str"	# str is read into an array as tokens separated by IFS
for i in "${ADDR[@]}"; do	# access each element of array
    echo "$i"
done
IFS=' '		# reset to default value after usage

Run the above bash shell script in terminal.

Output

$ ./bash-split-string-example-1 
Learn
to
Split
a
String
in
Bash
Scripting

The default value of IFS is single space ' '. We changed the value of IFS to split a string. It should not affect any further script that is dependent on IFS. So, assign the default value back to IFS.

Another safe style is to scope the IFS change to the read command itself. This is useful in larger scripts where changing IFS globally can affect later loops, reads, or expansions.

</>
Copy
#!/bin/bash

line="name,email,status"

IFS=',' read -r -a fields <<< "$line"

printf 'Name: %s\n' "${fields[0]}"
printf 'Email: %s\n' "${fields[1]}"
printf 'Status: %s\n' "${fields[2]}"
Name: name
Email: email
Status: status

Bash split string by comma and get a specific field

A common use case is to split a delimited value and read only one field. Bash arrays are zero-based, so after splitting "server01:8080:active", the host is at index 0, the port is at index 1, and the status is at index 2.

</>
Copy
#!/bin/bash

record="server01:8080:active"

IFS=':' read -r -a parts <<< "$record"

host="${parts[0]}"
port="${parts[1]}"
status="${parts[2]}"

echo "Host=$host"
echo "Port=$port"
echo "Status=$status"
Host=server01
Port=8080
Status=active

Before using an index in production scripts, check that the expected field exists. This avoids empty values when the input line is incomplete.

</>
Copy
#!/bin/bash

record="server01:8080"

IFS=':' read -r -a parts <<< "$record"

if [[ ${#parts[@]} -ge 3 ]]; then
    echo "Status=${parts[2]}"
else
    echo "Status field is missing"
fi
Status field is missing

Bash split string with multiple character delimiter

To split a string with a multiple character delimiter (or simply said another string), following are two of the many possible ways, one with idiomatic and the other with just basic bash if and bash while loop.

Do not use IFS="ABC" when the delimiter is the full string ABC. In that case Bash treats A, B, and C as separate delimiter characters. Use parameter expansion when the whole delimiter string must be matched.

Example 3: Split String with another string as delimiter idiomatic expressions

In this example, we will use idiomatic expressions where parameter expansion is done, and thus a very compact script.

Bash Script File

</>
Copy
#!/bin/bash

str="LearnABCtoABCSplitABCaABCString"
delimiter=ABC
s=$str$delimiter
array=();
while [[ $s ]]; do
	array+=( "${s%%"$delimiter"*}" );
	s=${s#*"$delimiter"};
done;
declare -p array

When you run the above script in a Bash Shell, you will see an output similar to the following

Output

$ ./bash-split-string-example-3
declare -a array='([0]="Learn" [1]="to" [2]="Split" [3]="a" [4]="String")'

Following Parameter-Expansions are used (Reference: Bash Man Page)

ExpansionDescription
${parameter%%word}Remove the longest matching suffix pattern.
${parameter#word}Remove shortest matching prefix pattern.

Example 4: Bash Split a String with multiple character delimiter

If you are new to bash shell scripting and are not familiar with idiomatic expressions, following is an easily understandable shell script with basic bash if, bash while and bash substring methods. Comments are provided at each step to make the script file more readable.

Bash Script File

</>
Copy
#!/bin/bash

# main string
str="LearnABCtoABCSplitABCaABCStringABCinABCBashABCScripting"

# delimiter string
delimiter="ABC"

#length of main string
strLen=${#str}
#length of delimiter string
dLen=${#delimiter}

#iterator for length of string
i=0
#length tracker for ongoing substring
wordLen=0
#starting position for ongoing substring
strP=0

array=()
while [ $i -lt $strLen ]; do
	if [ $delimiter == ${str:$i:$dLen} ]; then
		array+=(${str:strP:$wordLen})
		strP=$(( i + dLen ))
		wordLen=0
		i=$(( i + dLen ))
	fi
    i=$(( i + 1 ))
    wordLen=$(( wordLen + 1 ))
done
array+=(${str:strP:$wordLen})

declare -p array

Output

$ ./bash-split-string-example 
declare -a array='([0]="Learn" [1]="to" [2]="Split" [3]="a" [4]="String" [5]="in" [6]="Bash" [7]="Scripting")'

The split strings are stored in the array and could be accessed using an index.

The loop above is easy to read, but it is written for simple words. If your input may contain spaces, quote array appends in your own scripts, for example array+=("${str:strP:$wordLen}").

Bash split string using string replacement and mapfile

For a simple multi-character delimiter, another compact Bash approach is to replace the delimiter with newlines and then read those lines into an array. This is convenient when the delimiter is known and the field values themselves do not contain newlines.

</>
Copy
#!/bin/bash

str="red::green::blue"
delimiter="::"

mapfile -t colors <<< "${str//$delimiter/$'\n'}"

for color in "${colors[@]}"; do
    echo "$color"
done
red
green
blue

This replacement form uses Bash pattern matching in the search part. When the delimiter contains characters that are meaningful in Bash patterns, such as *, ?, or brackets, prefer a parsing loop where the delimiter is handled carefully.

Bash split string pitfalls with IFS, quotes, and empty fields

Most Bash split string errors come from changing IFS too broadly, forgetting to quote array values, or assuming every input has the same number of fields.

  • Scope IFS when possible: use IFS=',' read -r -a fields <<< "$line" or reset IFS after the split.
  • Quote array expansion: use "${fields[@]}" when looping so each field remains one item.
  • Remember zero-based indexes: the first split value is ${fields[0]}, not ${fields[1]}.
  • Do not use IFS for a full string delimiter: IFS="ABC" splits on A, B, or C, not on the complete token ABC.
  • Check empty and missing fields: adjacent delimiters or trailing delimiters may need special handling depending on how the data should be interpreted.

Bash split string FAQs

How do I split a string by delimiter in Bash?

For a single-character delimiter, set IFS to that delimiter and use read -r -a array_name <<< "$value". The split values are stored in a Bash array.

How do I split a Bash string by comma?

Use IFS=',' read -r -a fields <<< "$line". After that, use ${fields[0]}, ${fields[1]}, and so on to access individual comma-separated values.

Can I use IFS to split by a multiple-character delimiter in Bash?

No, not when the delimiter must be matched as one complete string. IFS treats each character in its value as a delimiter. For delimiters such as ABC or ::, use parameter expansion, string replacement with care, or a parsing loop.

How do I get the Nth element after splitting a string in Bash?

Split the string into an array and access the required zero-based index. For example, after splitting into parts, the third field is ${parts[2]}.

Why should I use read -r when splitting strings in Bash?

The -r option prevents backslash from being treated as an escape character. This is safer when input values may contain file paths, escape-like sequences, or literal backslashes.

Bash split string QA checklist

  • Confirm whether the delimiter is a single character or a full string before choosing IFS or parameter expansion.
  • Use read -r -a for array splitting and avoid unquoted array loops.
  • Verify the script with inputs that contain leading delimiters, trailing delimiters, adjacent delimiters, and missing fields.
  • Keep IFS changes local or reset IFS before later script logic runs.
  • Use output blocks only for command results and Bash code blocks only for executable script examples.

Summary: choosing the right Bash split string technique

In this Bash Tutorial, we have learned how to split a string using bash script with different scenarios based on delimiter: like single character delimiter and multiple character delimiter.

Use IFS with read -r -a for simple single-character delimiters such as spaces, commas, colons, and hyphens. Use parameter expansion or an explicit loop when the delimiter is a complete string such as ABC or ::. In both cases, quote array expansions and check field counts before using indexed values in longer scripts.