0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Split String by Delimiter

In Bash, you can split a string by a delimiter using IFS='delimiter' and read -ra array, for example: IFS=',' read -ra parts <<< "$string" splits $string by commas into the array parts.
📋

Examples

Inputapple,banana,cherry
Output["apple", "banana", "cherry"]
Inputone|two|three|four
Output["one", "two", "three", "four"]
Inputsingleword
Output["singleword"]
🧠

How to Think About It

To split a string by a delimiter in Bash, you set the Internal Field Separator (IFS) to that delimiter, then read the string into an array. This breaks the string wherever the delimiter appears, storing each part as an element in the array.
📐

Algorithm

1
Set the delimiter as the Internal Field Separator (IFS).
2
Use the read command with the -a option to read the string into an array.
3
Split the string by the delimiter into array elements.
4
Access or print the array elements as needed.
💻

Code

bash
#!/bin/bash
string="apple,banana,cherry"
IFS=',' read -ra parts <<< "$string"
for part in "${parts[@]}"; do
  echo "$part"
done
Output
apple banana cherry
🔍

Dry Run

Let's trace splitting 'apple,banana,cherry' by ',' through the code

1

Set string

string = "apple,banana,cherry"

2

Set IFS and read into array

IFS=',' read -ra parts <<< "$string" results in parts = ["apple", "banana", "cherry"]

3

Print each element

Outputs each element on a new line: apple, banana, cherry

IterationArray Element
1apple
2banana
3cherry
💡

Why This Works

Step 1: Set IFS to delimiter

The IFS variable tells Bash where to split the string, so setting IFS=',' means split at commas.

Step 2: Read string into array

Using read -ra array reads the input string into an array, splitting it by the IFS delimiter.

Step 3: Access array elements

Each part of the string becomes an element in the array, which you can loop over or access by index.

🔄

Alternative Approaches

Using parameter expansion and a loop
bash
#!/bin/bash
string="apple,banana,cherry"
delimiter=','
while [[ $string ]]; do
  part=${string%%$delimiter*}
  echo "$part"
  string=${string#*$delimiter}
  [[ $string == $part ]] && break
 done
This method uses string manipulation without arrays but is more complex and less readable.
Using 'cut' command
bash
#!/bin/bash
string="apple,banana,cherry"
echo "$string" | cut -d',' -f1
This extracts only one field at a time, so it's less flexible for splitting into multiple parts.

Complexity: O(n) time, O(n) space

Time Complexity

Splitting the string requires scanning each character once, so time grows linearly with string length.

Space Complexity

An array stores each split part, so space grows with the number of parts created.

Which Approach is Fastest?

Using IFS with read -ra is efficient and simple; alternatives like loops or external commands are slower or more complex.

ApproachTimeSpaceBest For
IFS and read -raO(n)O(n)Simple, efficient splitting into array
Parameter expansion loopO(n)O(1)Manual control, no arrays, more complex
cut commandO(n)O(1)Extracting single fields, not full splitting
💡
Always set IFS locally before reading to avoid affecting other parts of your script.
⚠️
Forgetting to set IFS causes the string not to split as expected, returning the whole string as one element.