TL;DR
To bash split string, follow these steps:
- Declare the string to split
string="Ubuntu:Debian:Fedora"
- Set the delimiter to colon
IFS=':'
- Split the string into an array
read -r -a array <<< "$string"
- Echo the array elements
echo "${array[0]}, ${array[1]}, ${array[2]}"
Splitting strings in Bash can be a common yet tricky task. Whether you’re parsing user input, processing data files, or automating tasks, there’s a solution that can make your life easier. This post will guide you through various methods to bash split string, offering step-by-step instructions and best practices. You’ll learn how to use commands like read
, cut
, awk
, and sed
for efficient string manipulation. By the end, you’ll be equipped with multiple techniques to handle string manipulation in your scripts effectively.
Why Split Strings?
Splitting strings in Bash scripts is useful for processing data, parsing input, and manipulating text. It allows you to handle and work with individual parts of a string more effectively.
- Processing Data: Sometimes, data comes in a single string that contains multiple pieces of information separated by a specific character, like commas or colons. Splitting the string helps you handle each piece of information separately.
- Parsing Input: When you get input from users or read from a file, the input might be a long string with different parts. By splitting the string, you can easily work with each part.
- Manipulating Text: If you need to change or analyze parts of a string, splitting it into smaller chunks makes the task simpler. For example, you can split a sentence into words to count them or find specific words.
- Formatting Output: Sometimes you need to format output in a specific way. Splitting a string allows you to reassemble the parts in a different order or format.
- Automating Tasks: Scripts often need to automate tasks that involve working with lists of items. Splitting a string into an array lets you loop through each item and perform actions on them.
How to Bash Split String?
To split string Bash, you can use the read
command along with the Internal Field Separator (IFS
). The IFS
is a special variable that determines how Bash recognizes field boundaries when it interprets strings. For example, if you want to split a string by spaces, you can set IFS
to a space and use read
to assign each segment to a variable. Here’s a basic example:
string="split this string"
IFS=' ' read -r part1 part2 part3 <<< "$string"
echo "First part: $part1"
echo "Second part: $part2"
echo "Third part: $part3"
This script splits the string “split this string” into three parts using space as a delimiter and assigns each part to a separate variable. You can adjust the IFS
to split by other characters, such as commas or colons, by changing its value accordingly.
Keep reading for nine different methods to split strings in Bash scripts:
1. Internal Field Separator (IFS) and read
The Internal Field Separator (IFS) and read
command method is ideal for parsing strings into arrays or variables based on a delimiter. This approach is especially useful for scripts that require handling lists or configurable input data. Here is the step-by-step guide to bash split string into array using the read command:
- Start by declaring the string you wish to split.
string="Ubuntu:Debian:Fedora"
This command initializes a string variable with a colon-separated list.

- Assign your delimiter to the IFS variable. For a colon-separated list:
IFS=':'
It will set the colon (:) as the delimiter for splitting the string.

- Use the read command with -r to prevent backslash escapes and -a to specify the array.
read -r -a array <<< "$string"
The command reads the string into an array, splitting it at each instance of the delimiter.

- You can now iterate over the array or access elements directly.
echo "${array[0]}, ${array[1]}, ${array[2]}"
It will echo the first three elements of the array, showing the split results.

2. cut Command
The cut command is straightforward and powerful for extracting sections from strings based on a delimiter. It’s perfectly suited for simple extractions where you know the field position of the data you need. Follow these steps for bash string split:
- Initialize your string variable.
string="Ubuntu,Debian,Fedora"
A comma-separated list for processing.

- Specify the delimiter with -d and the field number with -f.
echo "$string" | cut -d',' -f1
This command outputs the first field, in this case, Ubuntu.

3. Parameter Expansion
Parameter expansion offers a built-in way to manipulate strings based on patterns. It’s useful for removing known prefixes or suffixes without needing external commands. Here is how to do bash string parsing:
- Declare the String:
string="Ubuntu-Debian-Fedora"
A string variable with dash-separated values.

- Remove parts of the string using parameter expansion.
- To remove everything after the first dash:
prefix="${string%%-*}"

- To remove everything before the first dash:
suffix="${string#*-}"

- Display the results by running the command:
echo "Prefix: $prefix, Suffix: $suffix"
The output will look like this:

4. awk Command
awk
is a text processing powerhouse, capable of splitting strings based on a delimiter while also performing complex processing tasks. This method is best for scripts that require more than simple extraction. Follow these steps to bash split string by string using the awk
command:
- Initialize the String:
string="Ubuntu:Debian:Fedora"
A colon-separated list for splitting.

- Use -F to set the delimiter and {print} to output the fields.
echo "$string" | awk -F':' '{print $1, $2, $3}'
Each field is printed, separated by spaces.

5. tr Command for Character Replacement
The tr command is used for translating or deleting characters in a string. By replacing delimiters with newlines, tr effectively splits a string into multiple lines, which can be useful for multiline processing. Here is the step-by-step guide to string split bash:
- Set Up Your String:
string="Ubuntu;Debian;Fedora"
Initializes a semicolon-separated list for transformation.

- Use
tr
to replace the semicolon (;) with a newline (\n).
echo "$string" | tr ';' '\n'
The output presents each item on a new line, ready for looping or further processing.

6. Using read Command
The read command can be used with a delimiter to split a string directly into separate variables.
- Set the string and define the IFS variable to use the desired delimiter.
string="apple:banana:cherry"
IFS=':'
This sets the string and delimiter to a colon.

- Use the read command to split the string into individual variables.
read -r part1 part2 part3 <<< "$string"
This splits the string into three parts, stored in part1, part2, and part3.

- Access the individual elements of the array.
echo "$part1"
echo "$part2"
echo "$part3"

7. Using sed Command
The sed command is a stream editor that can be used for text manipulation, including splitting strings.
- Define the string you want to split.
string="apple,banana,cherry"

- Use the sed command to replace the delimiter with a newline character.
echo "$string" | sed 's/,/\n/g'

8. Using grep Command with Perl Compatible Regular Expressions (PCRE)
The grep command with the -oP option can be used to match and split strings based on patterns.
- Define the string you want to split.
string="apple,banana,cherry"

- Use the grep command with the -oP option to extract parts based on a pattern.
echo "$string" | grep -oP '[^,]+'
This matches and prints each part of the string.

9. Using while Loop with read
A while loop with the read command can be used to read each part of a string one by one based on a delimiter.
- Define the string you want to split and set the IFS variable to the desired delimiter.
string="apple,banana,cherry"
IFS=','

- Use a while loop to read each part of the string one by one.
echo "$string" | while read -d',' part; do
echo "$part"
done
This reads and prints each part of the string in a loop.

Advanced Techniques to Use When Splitting String in Bash
Mastering string splitting in Bash scripting can greatly enhance data processing capabilities. If you want to explore more, here are three advanced techniques that offer greater flexibility and power in handling complex string manipulation tasks:
- 🧬 Pattern Matching with
sed
orperl
: Usesed
for straightforward pattern-based splitting andperl
for complex manipulations. Both excel in transforming strings through regular expressions, ideal for tasks like reformatting data or extracting specific segments. - 🎯 Utilizing grep and awk for Regex: Leverage
grep
to isolate specific data segments andawk
for its text processing power. They offer advanced pattern matching, enabling conditional data processing and sophisticated parsing operations within scripts. - 📚 Using Bash Arrays Effectively: Leverage Bash arrays for storing split components, especially useful for strings that require splitting into multiple parts. Arrays offer flexibility in accessing, manipulating, and iterating over data elements.
3 Best Practices in String Splitting
String splitting is a crucial aspect of Bash scripting, enabling efficient data manipulation and processing. To maximize effectiveness and maintain script clarity, adhering to best practices is essential. Here are three best practices to follow:
- 🛠 Choose the Right Tool for the Job: Depending on your specific needs—whether simple extractions or complex parsing—select a method (e.g.,
awk
,cut
, IFS) that best matches the task’s complexity and performance requirements. - 📏 Handle Delimiters Carefully: Always verify the presence and uniqueness of delimiters within your string. Inconsistent or missing delimiters can lead to unexpected results, so consider edge cases and potentially ambiguous data.
- 🔄 Test with Different Data Sets: Run your script against various input sizes and formats to ensure reliability. This helps identify any limitations or bugs in your approach, especially for scripts intended for general use.
Final Thoughts
In this article, I’ve walked you through step-by-step methods to split strings in Bash, including using read
with IFS, cut
, awk
, and sed
. I’ve also shared advanced techniques and best practices to help you efficiently handle string manipulation in your scripts. Mastering these methods will greatly enhance your Bash scripting skills.
For further learning, I suggest exploring:
- Methods to use the
read
command in Linux, which offers more advanced uses ofread
for string handling. - Another valuable read is about finding the length of a string in Bash, which is essential for string manipulation and validation.
- Additionally, understanding how to replace a substring with another string in Bash will deepen your ability to modify text efficiently.
Frequently Asked Questions
What Is Internal Field Separator (IFS)?
read
and for
, interpret data and text.By default, IFS is set to whitespace characters: space, tab, and newline. This setting allows commands to treat spaces, tabs, and newlines as field separators. However, you can modify IFS to use different delimiters, such as commas, colons, or any other character, depending on the requirements of your script. Changing the IFS allows for flexible text processing and data manipulation in shell scripting.
What is the cut Command in Linux?
cut
command in Unix and Linux is a text processing utility that is used to extract sections of text from each line of input, typically from a file or piped from another command. The command is highly useful for extracting column-based data in a table or any structured text.It allows you to specify which bytes, characters, or fields to extract from each line using delimiters specified by the -d option and fields indicated by the -f option. For example:
1. To extract the first column from a comma-separated file, you would use: cut -d’,’ -f1 filename.csv
2. To extract the first and third columns, you would use: cut -d’,’ -f1,3 filename.csv
3. To extract characters from positions 1 to 5 in each line of a text file, you would use: cut -c1-5 filename.txt
These capabilities make cut an essential tool for shell scripting and command-line data manipulation.
What Is awk Command?
awk
command in Linux is a powerful text-processing tool primarily used for pattern scanning and processing. It is highly useful for data extraction and reporting when working with text files. The command enables users to define actions to perform on text that matches specific patterns, making it ideal for transforming data, generating reports, and even performing basic arithmetic operations. Its versatile programming language features, including variables, numeric functions, string functions, and logical operators, awk, enhance the capability of Unix-like operating systems to handle complex text-processing tasks efficiently.How can I handle strings with mixed delimiters in Bash?
awk
or sed
coupled with regular expressions. This approach enables flexible splitting across various delimiters within the same string, offering a robust solution for complex string manipulation tasks.Can I split a string based on a pattern instead of a fixed delimiter?
awk
, sed
, or perl
. These tools empower you to employ complex regular expressions for identifying and splitting strings based on patterns, enhancing the flexibility and precision of string manipulation.What method should I use to split very long strings without losing performance?
awk
is highly recommended due to its effectiveness and reduced resource consumption. As a tool designed for processing text streams, awk
excels in managing substantial data volumes, ensuring optimal performance during string-splitting operations.Is it possible to split a string into variables instead of an array?
read
command along with the Internal Field Separator (IFS) to divide the string, thereby assigning each segment to distinct variables. This technique is particularly useful for scripts dealing with structured input formats.