HOWTO · Bash
How to Split String Into Variables in Bash
Split a delimited string into Bash variables with cut, IFS and read, sed, or regular-expression matching.
On this page
To split a delimited string into named variables in Bash, choose a method that matches the delimiter: cut selects fields for one delimiter, read with IFS assigns fields directly, and [[ ... =~ ... ]] captures fields with a regular expression. The examples below show how to adapt each approach and where it fits.
Strings in Bash
Bash stores text in string variables. A string can contain letters, digits, punctuation, and spaces; a delimiter is simply the character or sequence you choose to separate its fields. For example, in blue:large:cotton, the colons mark three fields. Bash does not require a separate string declaration, so assign the text with record="blue:large:cotton" and quote variable expansions such as "$record" to preserve the value as one argument.
String Functions in Bash
This article focuses on splitting rather than general string operations. The key distinction is between selecting fields and matching a pattern: cut selects numbered fields using one delimiter, IFS tells read which characters separate fields, and Bash’s =~ operator lets a regular expression describe the parts to capture. Choose based on whether the input has a simple separator, several separator characters, or a structure that needs a pattern.
Split String Into Variables in Bash
For a simple delimiter, assign the separated fields to named variables. This self-contained example uses cut to select three hyphen-separated fields, then uses read to assign them:
record="0123-456-789"
fields=$(printf '%s\n' "$record" | cut -d '-' -f 1-3)
IFS='-' read -r first second third <<< "$fields"
printf 'First: %s\nSecond: %s\nThird: %s\n' "$first" "$second" "$third"
Use the cut Command to Split String Into Variables in Bash
The cut command’s -d option sets one delimiter character, and -f selects one or more field numbers. In the example above, -f 1-3 asks cut for the first three fields. Capturing the result and passing it to read assigns those fields to shell variables. Quote the input expansion and use printf to pass the record as one predictable line.
Use cut when records have a consistent single-character separator and you need selected fields. It does not interpret multiple different delimiters as equivalent, and it leaves the original record unchanged. If the delimiter is absent, cut passes the line through; therefore field selection does not validate that a record has the expected shape.
Use an Internal Field Separator (IFS) to Split String Into Variables in Bash
read splits its input using IFS, the Internal Field Separator. Set IFS for that one command to avoid changing the shell’s later splitting behavior. This example treats hyphens and commas as separators:
record="red-blue,green"
IFS='-,' read -r first second third <<< "$record"
printf 'First: %s\nSecond: %s\nThird: %s\n' "$first" "$second" "$third"
The -r option keeps backslashes literal, while quoting "$record" protects the input. Each character in IFS is a delimiter; it is not a multi-character string that must appear together. Because the delimiters here are non-whitespace, adjacent separators can represent an empty field. If you provide fewer variable names than fields, the final variable receives the remaining input, including separators between the remaining fields. By default, IFS contains space, tab, and newline, which read treats as whitespace separators and trims at the ends.
Use read With sed to Split String Into Variables in Bash
Use sed to transform each matched character into a character followed by a marker, then let read split on that marker. Unlike the default-whitespace approach, this preserves spaces from the original string:
record="A B!"
transformed=$(printf '%s' "$record" | sed 's/./&|/g')
IFS='|' read -r -a characters <<< "$transformed"
printf 'First: %s\nSecond: %s\nThird: %s\nFourth: %s\n' "${characters[0]}" "${characters[1]}" "${characters[2]}" "${characters[3]}"
Here sed appends | after each match and read -a stores the resulting fields in an indexed array. The marker is only a separator introduced for this transformation; it is not a general-purpose way to parse structured records. The meaning of . in sed can depend on the active locale, so this technique is best suited to simple text where that behavior is acceptable. A space inserted as a separator would be discarded by default IFS whitespace handling, which is why this example chooses | and sets IFS explicitly.
Use a Regular Expression With Match to Split String Into Variables in Bash
Bash’s [[ string =~ regex ]] conditional checks whether a string matches a regular expression. Parenthesized groups are available in BASH_REMATCH, with element zero holding the full match and elements starting at one holding captured groups. Keep the regular expression in an unquoted variable on the right side of =~ so Bash treats it as a pattern:
record="31-28-31"
pattern='^([^-]+)-([^-]+)-([^-]+)$'
if [[ $record =~ $pattern ]]; then
first=${BASH_REMATCH[1]}
second=${BASH_REMATCH[2]}
third=${BASH_REMATCH[3]}
printf 'First: %s\nSecond: %s\nThird: %s\n' "$first" "$second" "$third"
fi
record="high,risk"
pattern='^([^,]+),([^,]+)$'
if [[ $record =~ $pattern ]]; then
first=${BASH_REMATCH[1]}
second=${BASH_REMATCH[2]}
printf 'First: %s\nSecond: %s\n' "$first" "$second"
fi
The first pattern captures exactly three non-hyphen fields; the second captures two comma-separated values. Adjust the groups when the expected number or shape of fields changes. A match is not guaranteed, so place assignments that use BASH_REMATCH inside the successful branch. This method is useful when a delimiter rule needs validation or several parts must be captured at once; for a straightforward fixed delimiter, IFS or cut is shorter.