Bash

How to split one string into multiple strings separated by at least one space in bash shell

25 September 2026 · 5 min read

How to split one string into multiple strings separated by at least one space in bash shell

Bash, the ubiquitous shell in Unix-like systems, offers a powerful toolkit for manipulating text. One common task is splitting a string into multiple strings based on delimiters, and spaces are frequently used for this purpose. Mastering this technique is crucial for anyone working with text data in a bash environment, from system administrators to data scientists. This article provides a comprehensive guide on how to efficiently split strings in bash, separated by one or more spaces, empowering you to handle text manipulation tasks with ease.

Using the IFS Variable

The Internal Field Separator (IFS) variable is a powerful tool in bash for controlling how the shell splits words into fields. By default, IFS is set to whitespace (spaces, tabs, and newlines). You can leverage this default behavior to split strings based on spaces.

Consider the string “apple banana orange”. Using the read command with the -a option (for array) allows us to store the split words into an array:

string="apple banana orange" read -ra words <<< <<(echo "$string") for word in "${words[@]}"; do echo "$word" done 

This code snippet demonstrates how read -ra utilizes the IFS variable to split the string into individual words, storing them in the words array. Notice how consecutive spaces are treated as a single delimiter – this is the default IFS behavior.

Utilizing the set Command

Another approach to splitting strings based on spaces involves the set command. This built-in command can modify positional parameters, which are essentially variables representing the arguments passed to a script or function.

The following example illustrates the use of set:

string="apple banana orange" set -- $string for word; do Iterates over positional parameters echo "$word" done 

Here, set -- $string assigns each space-separated word in the string to a positional parameter. The subsequent loop iterates through these parameters, printing each word. Similar to the read method, multiple spaces are treated as single delimiters.

Advanced Splitting with Regular Expressions

For more complex splitting scenarios involving patterns beyond simple spaces, regular expressions can be utilized with tools like grep or awk.

Here’s an example using grep:

string="apple-banana orange.grape" grep -oE '[^[:space:]]+' <<< <<(echo "$string") 

The -o option tells grep to print only the matching part of the line, and -E enables extended regular expressions. The expression [^[:space:]]+ matches one or more non-whitespace characters, effectively splitting the string based on one or more spaces. This approach provides flexibility for more intricate string manipulations.

Handling Edge Cases and Best Practices

When dealing with strings containing special characters or leading/trailing spaces, additional care is required. Quoting variables is essential to prevent word splitting and globbing issues. For instance, always use double quotes around variables like "$string".

Consider a string with leading spaces: " apple banana". Without proper quoting, the leading spaces might be interpreted differently. Using the read method with proper quoting handles this correctly:

string=" apple banana" read -ra words <<< <<(echo "$string") 

For situations requiring specific handling of consecutive spaces or other delimiters, the tr command can be useful to replace multiple spaces with single spaces before splitting.

Here is a real-world example of how string splitting can be used in a bash script to process log files. Imagine a log file where each line contains a timestamp, a user ID, and a message, separated by spaces. You could use string splitting to extract these components for analysis or reporting.

  • Always quote variables (e.g., "$string") to prevent unintended word splitting.
  • Use tr to normalize spaces if needed before splitting.
  1. Identify the delimiter (in this case, spaces).
  2. Choose an appropriate method (IFS, set, regular expressions).
  3. Implement the chosen method in your bash script, considering edge cases.

Check out this helpful resource on bash scripting: Bash Manual.

“String manipulation is a fundamental skill for any programmer, and bash provides versatile tools for efficiently handling these operations,” says renowned shell scripting expert, Greg Wooledge.

[Infographic placeholder: illustrating different string splitting methods]

Learn More About BashEfficiently splitting strings in bash, based on spaces, is a fundamental skill for effective text manipulation. By understanding and utilizing the methods described in this guide, you can streamline your bash scripts and enhance your data processing capabilities. Whether you’re using the IFS variable, the set command, or regular expressions, choosing the right approach for your specific needs is key to successful string splitting. Remember to consider edge cases and employ best practices, such as quoting variables, to ensure accurate and reliable results.

For further exploration, consider diving deeper into regular expressions and advanced bash scripting techniques. Resources like the Regular-Expressions.info website and the Advanced Bash-Scripting Guide offer valuable insights. Start practicing these techniques today to elevate your bash scripting skills and unlock the full potential of text manipulation in your projects. Explore topics such as array manipulation and advanced regular expressions to further enhance your text processing capabilities.

FAQ

Q: How do I handle strings with leading or trailing spaces?

A: Ensure proper quoting of variables (e.g., "$string") when using the read or set commands. This prevents the shell from interpreting the leading/trailing spaces incorrectly.

Question & Answer :
I have a string containing many words with at least one space between each two. How can I split the string into individual words so I can loop through them?

The string is passed as an argument. E.g. ${2} == "cat cat file". How can I loop through it?

Also, how can I check if a string contains spaces?

I like the conversion to an array, to be able to access individual elements:

sentence="this is a story" stringarray=($sentence) 

now you can access individual elements directly (it starts with 0):

echo ${stringarray[0]} 

or convert back to string in order to loop:

for i in "${stringarray[@]}" do : # do whatever on $i done 

Of course looping through the string directly was answered before, but that answer had the the disadvantage to not keep track of the individual elements for later use:

for i in $sentence do : # do whatever on $i done 

See also Bash Array Reference.