Home » Linux » Shell » Bash Split String

Bash Split String (+ Shell Scripts)

This article will show you how to split a string at a given delimiter in Bash/Shell scripts and show some examples.

Splitting strings is a handy function to have available when crafting your scripts. CSV (Comma Separated Values) is a common format in which data is made available online, where data fields in a table are separated by (surprise) commas.

You may also simply be looking to split a sentence into words at the spaces, or split paragraphs into sentences at the period, and so on.

Many solutions focus on Bash-specific methods of splitting strings. Below, I’ll outline a method that should work in any Linux shell script.

Using the cut Command to Split Strings

The cut command in Linux removes sections from lines in a text file and then outputs them.

This example will adapt this command to split text at a given delimiter.

For the full user manual of the cut command, click here.

The Code

Below is an example Bash script which takes the string splitMe and returns items based on their position in the string split at the commas (,):

#!/bin/bash

# Define a comma-separated string
splitMe='apple,banana,grape,kiwi'

# Get the first item in the split string
firstItem="$(echo $splitMe | cut -d',' -f1)"

# Get the third item in the split string
thirdItem="$(echo $splitMe | cut -d',' -f3)"

# Confirm the result by outputting it to screen
echo $thirdItem

So why does this work? The echo command is used to pipe the original string to the cut command. The cut command uses the -d option to specify the delimiter (in this case a comma, but any character or string can be used), and the -f option followed by a number to specify which field should be returned – that’s the position of the item you wish to get from the split string.

Reusable Bash Function

That’s all well and good, but it’s not really re-usable – so here it is in a function:

#!/bin/bash

# Define function to split strings
# It will accept three parameters, the string to split, the delimiter, and finally the position of the item to return
splitMyString(){
    splitString=$1
    delimiter=$2
    item=$3
    result="$(echo $splitString | cut -d',' -f$item)"
    echo $result
}

# Define a string to split for testing
splitMe='apple,banana,grape,kiwi'

# Test the function by splitting the string at the comma and returning the second item
echo $(splitMyString $splitMe "," 2)

Conclusion

And there you have it. You have learned to split a string in bash at a specific delimiter.

If you found this guide or have any additional questions about splitting strings with bash, then please leave a comment in the comment section below.

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment