How do I split a string on a delimiter in Bash?
Categories:
Mastering String Splitting in Bash

Learn various techniques to split strings by delimiters in Bash, from basic internal field separators to advanced regex-based methods.
Splitting a string by a delimiter is a fundamental operation in shell scripting. Whether you're parsing log files, processing command output, or extracting data from configuration files, knowing how to effectively break down a string into its constituent parts is crucial. Bash offers several powerful methods to achieve this, each with its own advantages and use cases. This article will guide you through the most common and effective techniques, helping you choose the right tool for your specific needs.
Using Internal Field Separator (IFS)
The IFS
(Internal Field Separator) variable is a special Bash variable that defines the character(s) used for word splitting. By default, IFS
contains space, tab, and newline characters. You can temporarily change IFS
to your desired delimiter to split a string into an array.
#!/bin/bash
MY_STRING="apple,banana,orange,grape"
DELIMITER=","
# Save original IFS and set new IFS
OLD_IFS=$IFS
IFS=$DELIMITER
# Read the string into an array
read -ra ADDR <<< "$MY_STRING"
# Restore original IFS
IFS=$OLD_IFS
# Iterate and print array elements
for i in "${ADDR[@]}"; do
echo "Item: $i"
done
# Example with a different delimiter
PATH_STRING="/usr/local/bin:/usr/bin:/bin"
IFS=':' read -ra PATH_PARTS <<< "$PATH_STRING"
echo "\nPath components:"
for p in "${PATH_PARTS[@]}"; do
echo "- $p"
done
Splitting a string using IFS and read -ra
IFS
value when you modify it. This prevents unexpected behavior in subsequent commands or scripts that rely on the default IFS
.Parameter Expansion with Substring Removal
For simpler cases where you only need the first or last part of a string after a split, or to iterate through parts without explicitly creating an array, parameter expansion offers a concise solution. This method doesn't directly 'split' into an array but allows you to extract portions based on a pattern.
#!/bin/bash
FILE_NAME="document.report.2023.txt"
# Get filename without extension
BASE_NAME="${FILE_NAME%.*}"
echo "Base Name: $BASE_NAME"
# Get only the extension
EXTENSION="${FILE_NAME##*.}"
echo "Extension: $EXTENSION"
# Get the first part before the first dot
FIRST_PART="${FILE_NAME%%.*}"
echo "First Part: $FIRST_PART"
# Iterating through parts using a loop and parameter expansion
IP_ADDRESS="192.168.1.100"
TEMP_IP=$IP_ADDRESS
echo "\nIP Address Parts:"
while [[ $TEMP_IP =~ \. ]]; do
PART="${TEMP_IP%%.*}"
echo "- $PART"
TEMP_IP="${TEMP_IP#*.}"
done
echo "- $TEMP_IP" # Print the last part
Using parameter expansion for substring removal and iteration
Using awk
for Advanced Splitting
When you need more powerful text processing capabilities, especially with complex delimiters or field manipulation, awk
is an excellent choice. awk
can easily split lines into fields based on a specified field separator (-F
option or FS
variable) and allows you to process each field individually.
#!/bin/bash
CSV_LINE="ID123,John Doe,john.doe@example.com,Active"
echo "Splitting with awk:
"
# Split by comma and print each field
awk -F',' '{ for (i=1; i<=NF; i++) print "Field " i ": " $i }' <<< "$CSV_LINE"
# Split by space and print specific fields
SENTENCE="This is a sample sentence"
awk '{ print "First word: " $1 ", Last word: " $NF }' <<< "$SENTENCE"
# Using a regex as a delimiter (e.g., one or more spaces/tabs)
DATA_LINE="Item 123 Value XYZ"
awk -F'[ \t]+' '{ print "Item Code: " $1 ", Quantity: " $2 ", Status: " $3 }' <<< "$DATA_LINE"
Splitting strings using awk
with various delimiters
flowchart TD A[Start] B{Choose Method} C[Simple Delimiter?] D[Need Array?] E[Complex Delimiter/Regex?] F[Only First/Last Part?] G[Use IFS + read -ra] H[Use awk -F] I[Use Parameter Expansion] J[End] A --> B B --> C C -- Yes --> D D -- Yes --> G D -- No --> F F -- Yes --> I F -- No --> G C -- No --> E E -- Yes --> H E -- No --> G G --> J H --> J I --> J
Decision flow for choosing a string splitting method in Bash
Practical Steps for String Splitting
Here's a general approach to splitting strings in Bash, combining the techniques discussed.
1. Identify Your Delimiter
Determine the character or pattern that separates the parts of your string. This could be a comma, space, colon, or a more complex regular expression.
2. Choose the Right Tool
If you need to iterate over all parts and store them in an array, IFS
with read -ra
is often the most direct Bash-native way. For extracting specific parts or simple pattern matching, parameter expansion is efficient. For advanced parsing, regex delimiters, or field manipulation, awk
is superior.
3. Implement the Split
Write the code using the chosen method. Remember to handle IFS
carefully by saving and restoring it if you modify it globally. Test with edge cases like empty fields or delimiters at the beginning/end of the string.
4. Process the Parts
Once the string is split, iterate through the resulting array or fields to perform your desired operations, such as printing, storing, or further processing.
IFS
might not behave as expected, and awk
with a more specific field separator or quoting might be necessary.