Bash Scripting
  • scripting list
  • How to Make Bash Script Executable Using Chmod
  • Shell Script to Check Linux Server Health
  • How to Concatenate String Variables in Bash [Join Strings]
  • How to Do Bash Variable Substitution (Parameter Substitution)
  • Bash Parameter Expansion with Cheat Sheet
  • Bash getopts with Examples
  • How to Pass all Arguments in Bash Scripting
  • Bash Function Return Value
  • Bash Loop Through Lines in a File
  • Bash readarray with Examples
  • Bash let with Examples
  • Bash expr with Examples
  • Bash read password
  • Bash for Loop Range Variable
  • Bash Arrays Explained
  • Ways to Find Bash Array Length
  • Bash Split String by Delimiter
  • Bash if Options
  • Bash If Statements for String Comparison
  • Debugging Techniques for Bash Scripts
  • Determining if a Bash String is Empty
  • Bash if Statement with Multiple Conditions
  • Meaning of Shebang in Bash Scripts
  • How to Comment Code in Bash Script
  • How to Read CSV File in Bash
  • Bash Scripting: How to Check if File Exists
  • Bash If Else Statements: Examples and Syntax
  • Bash Scripting: How to Check if Directory Exists
  • Bash eval Command with Examples
  • How to Use Sleep Command in Bash Scripting
  • Bash Associative Arrays with Examples
  • Bash Script for Counting Lines in a File
  • How to Use While Loop in Bash for Efficient Scripting
  • Bash basename Command with Examples
  • How to Create Multiline Strings in Bash
  • How to Use Bash if With && Operator
  • 50 Bash Script Examples to Kickstart Your Learning
  • Case statement in Bash Shell Scripting
  • Trimming White Space in Bash
  • How to Extract Filename from Absolute Path in Bash
  • How to Get Directory Path in Bash
  • Extract Extension from File Path in Bash
  • Extract Filename without Extension from Full Path in Bash
  • Bash for Each File in a Directory
  • Bash for Loop with Array
  • Bash Continue – Using with for Loop
  • Bash Backticks vs Dollar Parentheses $()
  • How to Assign Variable in Bash
  • How to Assign Variable in Bash
  • Bash Division Explained
  • Bash Modulo (Division Remainder)
  • Bash While Read Line by Line
  • Bash shift Command
  • Bash Looping Through Array of Strings
  • Bash read Command with Examples
  • Bash Check Empty Array
  • Using Bash For Loops to Iterate Over a List of Strings
  • Bash Break – Using with For Loop
  • How to Use seq With for Loop in Bash
  • How to Use $@ in Bash Scripting
  • Get the Current Script Directory in Bash
Powered by GitBook
On this page
  • 1. Using $IFS variable
  • 2. Using read command
  • 3. Using tr command
  • 4. Using awk command
  • 5. Using cut command
  • 6. Using sed command
  • Conclusion

Bash Split String by Delimiter

PreviousWays to Find Bash Array LengthNextBash if Options

Last updated 1 year ago

Splitting string data is essential in some specific tasks, such as reading log files line by line to get specific information like date. Most programming languages have a built-in function known as 'split' for dividing string data into various parts.

On the other hand, the bash does not contain such built-in functions. Therefore, it uses delimiters for splitting string data. Delimiters are often either a special character or a sequence of characters separating parts of a string.

In this tutorial, we will learn various ways to split string data using different examples.

1. Using $IFS variable

The special variable $IFS is called Internal Field Separator. It is used in bash to split a string into words by assigning a specific delimiter. White space is the default delimiter for $IFS. However, other values such as '\n', '\t', '-' etc. can be used.

After assigning the delimiter, the string can be read by two options: '-r' and '-a'. The first option, '-r', reads the backslash (\) as a character rather than escape character while the '-a' option stores the split-ted words into an array variable.

The examples below show different ways to split string data with $IFS.

Example 1: Bash Split String by Space

#!/bin/bash# Define the string valuemy_string="Welcome to Linux OS"# Setting the space delimiterIFS=' '# Read the split words into an array using the set delimiter (space)read -a strarr <<< "$my_string"# use for loop to print each value of the arrayfor word in "${strarr[@]}";do echo "$word"done

The script defines the string value to split and specifies the delimiter as space. The set delimiter is then used to split the words into an array which are then printed out using a for loop.

The output prints each of the split words of the provided string: “Welcome to Linux OS”. It shows that string splitting is based on the space delimiter.

Example 2: Bash Split String by a Particular Character

#!/bin/bash# Reading the string value providedecho "Input your First Name, Last Name and State separated by a comma: "read input# set the comma as a delimiterIFS=',' # read the string as an arrayread -a strarr <<< "$input" # Print the splitted wordsecho "First Name : ${strarr[0]}"echo "Last Name: ${strarr[1]}"echo "State: ${strarr[2]}"

The script prompts the user to key in specific input, reads the input, and sets the comma as a delimiter. It then reads the input string as an array (starr).

From the output, the user provided “John, Doe, Ohio” as their input. This string was then splitted based on the comma delimiter and the output printed.

2. Using read command

#!/bin/bash# Read the string value providedecho "Enter text with a colon (:) to split"read input# Split the provided text based on the colon delimiterreadarray -d : -t strarr <<< "$input"# Use for loop to print each value of the arrayfor (( i=0; i< ${#strarr[*]}; i++))do echo "${strarr[i]}"done

This script prompts the user to enter input with a colon. It reads the string provided into an array and splits it based on the colon delimiter. The for loop is then used to print each value of the array.

In this example, the string “Python:Node:React” is taken in as input for splitting. The readarray command used with the -d option splits the provided string data based on the colon (:) delimiter.

The read command reads raw input (option -r), it interprets backslashes literally rather than treating them as escape characters. The option -a with read command stores the word read into an array in bash. The bash loop prints the string in split form.

#!/bin/bash# Define a string valuemy_string="Windows;Linux OS; Debian; Fedora"# Set the delimiterIFS=';'# Split the string value providedread -ra strarr <<< "$my_string"# Use for loop to print the splitted stringfor i in "${strarr[@]}"do echo $idone

This script defines the string to split and sets the semicolon as the delimiter. The for loop is then used to read the value of the splitted string.

The output shows the results printed after splitting the string value, “Windows;Linux OS; Debian; Fedora”, based on the semicolon delimiter.

3. Using tr command

Instead of the read command, the tr command is used to split the string on the delimiter. The following example shows how this command is used.

#!/bin/bash# Define the string value to splitmy_string="Windows; Linux OS; Debian; Fedora"strarr=($(echo $my_string | tr ";" "\n"))# Use for loop to print the split stringfor n in "${strarr[@]}"do echo "$n"done

This example is pretty much the same as the previous one but instead of the read command the script uses the tr command.

In this approach, array elements are divided based on semicolon and space delimiters. The string “Windows; Linux OS; Debian; Fedora '' results in the output shown above. Some elements are treated as separate words. For example, ‘Linux OS’ is treated as two words: Linux and OS.

4. Using awk command

The awk Linux command works in bash and shell distributions. It returns the exit code with the result. The pipe (|) symbol is used to pass input to the awk command. Awk provides a split function to create an array based on the given delimiter.

The syntax for the split function is:

split(SOURCE, DESTINATION,DELIMITER)

However, the delimiter is optional and defaults to space if not provided.

Example:

#!/bin/bash# Define a string valuemy_string="9 10 11"# split the string provided.echo $my_string | awk '{split($0,b);print b[1]; print b[2]; print b[3]}'

In this script, the delimiter is not provided. Therefore, the string value defined is split based on the space delimiter.

The defined string value was “9 10 11”. This string is split based on the space delimiter and the output is printed as shown above.

5. Using cut command

The cut command can be used to split a string on a delimiter. The -d option is used to specify the delimiter. The -f option is used to specify the string to split.

#!/bin/bash# Define the string valuemy_string="Python;React;Angular"# Split the string provided on the semicolon (;) delimiterecho $my_string | cut -d ';' -f 1

The script shows how the cut command is used to split a string on a delimiter. The defined string value is splitted based on the semicolon delimiter.

In this example, the cut command is used to split the string “Python;React;Angular” based on the semicolon delimiter. The -d option specifies the delimiter which in this case is the semicolon(;). The -f option specifies that the first element (Python) should be extracted.

6. Using sed command

The sed command can also be used to split string data. The syntax for this command is:

sed ‘s/delimiter/\n/g’

#!/bin/bash# Define a string valuemy_string="Hello:World"# Split the string providedsed 's/:/\n/g' <<< $my_string

In this case, g stands for global. It means that the substitution has to be globally, that is, for any occurrence. The example above shows how sed command is used to split string data using colon as a delimiter.

The output shows that the defined string value, “Hello:World”, was split into two words: Hello and World, based on the colon delimiter.

Conclusion

Splitting strings is useful in some specific use cases. Unlike other programming languages, bash splits strings using delimiters with commands such as IFS variable, tr, cut, awk, and sed commands.

A string can also be divided into sections without using the $IFS variable. For instance, the command used with -d option can be used to split string data. Just like in $IFS, the -d command defines the separator character.

readarray
split based on the space delimiter using awk
splits it based on the colon delimiter
bash script to output - split string by a particular character
semicolon as the delimiter
split string data using colon as a delimiter using sed
bash script to output - split string by space
using cut command split based on the semicolon delimiter
using tr command