Bash Scripting
String Manipulation
Programming
Coding Tutorials
Array Basics

How to split a string into an array in Bash?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Bash, splitting a string into an array usually means choosing a delimiter and letting read -a populate array elements. The main trap is that Bash word splitting is sensitive to quoting and IFS, so the correct solution depends on whether you want to preserve spaces, empty fields, or special characters.

Use IFS with read -a for Delimiter-Based Splits

The most common Bash pattern is to set IFS for one command and read into an array.

bash
1#!/usr/bin/env bash
2
3input="apple,banana,cherry"
4IFS=',' read -r -a items <<< "$input"
5
6printf '%s\n' "${items[@]}"

This is compact and keeps the delimiter local to the read command instead of changing IFS globally for the whole script.

Split on Spaces Carefully

If the separator is whitespace, read -a still works, but remember that multiple spaces may collapse because Bash treats whitespace specially during word splitting.

bash
1#!/usr/bin/env bash
2
3input="one two three"
4read -r -a words <<< "$input"
5
6printf '%s\n' "${words[@]}"

This is fine for simple space-separated data, but not ideal when empty fields or repeated whitespace must be preserved exactly.

Preserve Delimiter Semantics Intentionally

For CSV-like data, IFS=',' read -a is acceptable only when the format is simple and unquoted. It is not a full CSV parser. If fields can contain quoted commas, embedded spaces, or escaping rules, Bash string splitting is the wrong tool and you should use a proper parser.

For simple delimited strings, though, the Bash approach is enough.

bash
1#!/usr/bin/env bash
2
3record="John,Doe,30,Canada"
4IFS=',' read -r -a person <<< "$record"
5
6echo "First: ${person[0]}"
7echo "Last: ${person[1]}"
8echo "Age: ${person[2]}"
9echo "Country: ${person[3]}"

Split into Characters When Needed

Bash does not have a built-in “split every character” operator, but a loop works cleanly.

bash
1#!/usr/bin/env bash
2
3input="hello"
4chars=()
5
6for ((i = 0; i < ${#input}; i++)); do
7  chars+=("${input:i:1}")
8done
9
10printf '%s\n' "${chars[@]}"

This is much more predictable than trying to force character splitting through shell word expansion.

Avoid Global IFS Changes When Possible

Older scripts often save and restore IFS manually. That works, but it is usually cleaner to scope the value to one read command.

bash
IFS='|' read -r -a parts <<< "$line"

That style avoids subtle bugs where later code unexpectedly uses the modified delimiter rules.

Know When Bash Is the Wrong Tool

If the input format becomes complex, especially CSV, JSON, or nested delimiters, Bash array splitting can become fragile very quickly. At that point, using awk, python, jq, or another parsing tool is usually safer than trying to make shell word splitting do more than it was designed for.

Bash is good at simple tokenization. It is not a general-purpose parsing language.

A small rule of thumb helps here: if the delimiter logic is simple, Bash can handle it; if quoting rules matter, move to a parser that was built for structured text.

Common Pitfalls

  • Changing IFS globally and forgetting to restore or limit its scope.
  • Failing to quote the input variable and accidentally triggering unwanted shell expansion.
  • Using Bash splitting for real CSV data that contains quoted commas or escaped values.
  • Expecting whitespace splitting to preserve empty fields exactly.
  • Using clever expansion tricks where a simple read -a or character loop would be clearer.

Summary

  • Use IFS=... read -r -a array <<< "$string" for simple delimiter-based splitting.
  • Keep IFS scoped to one command whenever possible.
  • Use a loop for character-by-character splitting.
  • Do not treat Bash splitting as a full CSV or structured-data parser.
  • Prefer simple, explicit Bash over brittle shell-expansion tricks.

Course illustration
Course illustration

All Rights Reserved.