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.
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.
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.
Split into Characters When Needed
Bash does not have a built-in “split every character” operator, but a loop works cleanly.
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.
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
IFSglobally 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 -aor character loop would be clearer.
Summary
- Use
IFS=... read -r -a array <<< "$string"for simple delimiter-based splitting. - Keep
IFSscoped 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.

