JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Splitting a string into variables in Bash

Author:JIYIK Last Updated:2025/03/23 Views:

This article will discuss different ways to split a string into variables in Bash.

We will start our discussion with a brief introduction to strings. Later, we will discuss various ways to split a string using Bash examples.


Strings in Bash

A string is a grouping/collection of characters. In Bash, a string is a data type similar to an integer or floating point number.

Characters may also include numbers, because we treat numbers in strings as sequences of ASCII characters.

In data types such as integers or floating point numbers, the number is a complete entity; there is no individual existence of a number. However, in a string, each character in the string (including spaces, commas, semicolons, and colons) has a representation.

Combined together, a single character also represents a complete string. Any character you can type from the keyboard can be part of a string.

For example:

s1="Welcome"
s2="This is a complete sentence and string as well"
s3="The string, has some special charactrs like ,$%^[]"
s4="The population of country ABC is 12345678"

Here, the first string has only letters. The following string (i.e. s2) also has spaces; these spaces are part of the string and will be stored like letters.

So, in the second string, there are eight spaces, which will take up eight bytes in memory.

The third string has special characters which are also part of the string.

Finally, in the fourth string we have a number which is not a number but a combination of numbers. If we want to calculate/compare it as a number, it is not possible unless it is converted to a number through some mechanism.


String Functions in Bash

Just like arithmetic operations on integers or floating point numbers, certain operations/functions are possible/available for strings.

For example, we can compare two strings for equality (if we have to find a string or substring, then equality is required). Here is the Bash script:

s1="World Cup."
s2="World Cup"

if [ "$s1" = "$s2" ]; then
    echo "Strings are equal."
else
    echo "Strings are not equal."
fi

s3="World Cup."

if [ "$s1" = "$s3" ];  then
    echo "Both the strings are equal."
else
    echo "Strings are not equal."
fi

Here we have two strings that appear to be identical; however, the dot at the end of the first string is different.

The above script first ifcompares two strings using the statement, where the string variables are enclosed in double quotes and compared using a single equal sign.

Next, it declares another string, s3, identical to the first, and performs the comparison again. The output of this script is:

Strings are not equal.
Both the strings are equal.

We can find the length of a string using the pound sign. The Bash script is:

s=abcAXYZ123456_AaBbCc
echo "Length of " $s " is:" ${#s}

The output of this script is:

Length of abcAXYZ123456_AaBbCc is: 20

We can use regular expressions to compare and get the length of the substrings (we won’t go into the details of regular expressions, you can read about regular expressions here).

Here is the code for matching using regular expressions:

s=abcAXYZ123456_AaBbCc
echo `expr match "$s" 'abc[A-Z]*.2'`
echo `expr "$s" : 'abc[A-Z]*.[0-9]*_'`

In the first match, we match lowercase abc, then an uppercase letter (zero or more), then the number 2.

The second match is done in a slightly different way, though, using a regular expression to match a substring with underscores in it.

The output of the above script is:

9
14

In the first match, the number 2 appears at position 9, and in the second match, the underscore appears at position 14. There is a long list of possible operations/functions on strings.

In the first match, the number two appears at position 9. In the second match, the underscore appears at position 14.

However, we are moving on to our topic, splitting a string.


Splitting a string into variables in Bash

Splitting a string into characters or substrings is a common and frequently used operation. For example, an expression needs to be split into variables and operators (called tokens) before it can be evaluated.

Before starting the translation phase, a compiler or other language translator uses a lexical analyzer to analyze the program, string, and split it into variables, keywords, blocks, functions, etc. In natural language processing, it is necessary to split the article into sentences, words, verbs, nouns, etc.

There are various ways to split a string in Bash. We will discuss them with examples.


Split a string into variables using cut command in Bash

In Bash, we can use the cut operator to split a string. The syntax of the cut command is:

cut -f(number) -d(delimiter)

cutThe command splits the string based on the delimiter provided after the -d option. The delimiter in the string can be any character that separates (or is assumed to separate) two substrings.

For example, in English, sentences are separated by dots; therefore, dots can be considered as separators that separate sentences. Similarly, spaces are separators that separate words.

Operators are separators between operands in arithmetic expressions (in programming).

Next, we have a script where we have a string with a hyphen as a delimiter. This string is split into three variables.

When we first use a variable, the dollar sign is not required; however, subsequent operations require a dollar sign with the variable. The script is:

v="0123-456-789"
v1=$(echo $v | cut -f1 -d-)
v2=$(echo $v | cut -f2 -d-)
v3=$(echo $v | cut -f3 -d-)
echo $v1
echo $v2
echo $v3

The output of this script is:

0123
456
789

We will use another delimiter with the cut operation to give you an idea. Here is another script with colon as delimiter:

v="0123:456:789"
v1=$(echo $v | cut -f1 -d:)
v2=$(echo $v | cut -f2 -d:)
v3=$(echo $v | cut -f3 -d:)
echo $v1
echo $v2
echo $v3

The code is the same except for the delimiter. The colon is the delimiter in the string; the same method is used in the cut operation as well.

The output of this script is:

0123
456
789

cutAn operation can choose a single delimiter; however, using the internal field separator, we can use multiple delimiters.


Splitting a string into variables using the Internal Field Separator (IFS) in Bash

Using IFS, we can use single or multiple delimiters. In case of single delimiter, no quotes are required; however, to use various delimiters, double quotes can contain multiple delimiters.

Let's look at a very basic example:

IFS=- read v1 v2 v3 v4 <<< this-is-batch-file
echo $v1
echo $v2
echo $v3
echo $v4

In the above script, IFS has only one delimiter. Using IFS, read assigns the string this-is-batch-file to variables v1 through v4.

The output of this script is:

this
is
batch
file

Next, we have an example of multiple delimiters. See the following script:

IFS=" ,: " read v1 v2 v3 v4 <<< is,something,strange:there
echo $v1
echo $v2
echo $v3
echo $v4

Here, two delimiters are used in IFS and string also contains two delimiters. The first three words are separated by commas and the last word is separated by a colon.

The output is:

is
something
strange
there

Now using IFS we can select multiple delimiters.

Do we always need to give a delimiter to split a string? The answer is no; we have other ways.


Splitting a string into variables using read and sed in Bash

We can split the string into an array of characters using read and sed (a special command that can perform many string operations line by line). So, once we have an array of characters, we can manipulate them according to our specific requirements.

Here is the script:

read -ra var5 <<<"$(echo "12-34-56" | sed 's/./& /g')"
echo ${var5[0]}
echo ${var5[1]}
echo ${var5[2]}
echo ${var5[3]}

The output of this script is:

1
2
-
3

请注意, even hyphens are placed as characters; therefore, this method cannot split the string on any delimiter. Instead, it breaks the string into an array of characters.

There is one exception to this approach. If you use spaces between characters, the process ignores the spaces and there are no spaces in the resulting array.

Let's understand this using the following script and adjacent output:

read -ra var51 <<<"$(echo "FG HI JK" | sed 's/./& /g')"
echo ${var51[0]}
echo ${var51[1]}
echo ${var51[2]}
echo ${var51[3]}

The output is:

F
G
H
I

In the output, subscript/index 2 obviously contains the letter H instead of a space character.


Split string into variables using regular expression with match in Bash

We have shared a link to read about Regular Expressions. Here we have another way to split a string using regular expressions and match operation using =~.

The script is:

re="^([^-]+)-(.*)$"
[[ "31-28-31" =~ $re ]] && var6="${BASH_REMATCH[1]}" && var_r="${BASH_REMATCH[2]}"
[[ $var_r =~ $re ]] && var7="${BASH_REMATCH[1]}" && var8="${BASH_REMATCH[2]}"
echo "First:" $var6
echo "Second:" $var7
echo "Third:" $var8

The hyphen is used as a delimiter again, and our regular expression contains a hyphen. The output is:

First: 31
Second: 28
Third: 31

Next, we use the same approach but with a different delimiter, the comma. The script is:

re1="^([^,]+),(.*)$"
[[ "high,risk" =~ $re1 ]] && v1="${BASH_REMATCH[1]}" && v2="${BASH_REMATCH[2]}"
echo "First:" $v1
echo "Second:" $v2

The output is:

First: high
Second: risk

Finally, we covered different ways to split a string in Bash and store it into variables. You are now well-positioned to use the method that suits your requirements.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Hosting Docker Internal in Linux

Publish Date:2025/03/23 Views:143 Category:OPERATING SYSTEM

Docker allows developers to efficiently build, test, and deploy applications by packaging them in standardized units called containers. When working with Docker containers, you may encounter scenarios where you need to connect a container t

Setting the working directory in Docker

Publish Date:2025/03/23 Views:198 Category:OPERATING SYSTEM

If present, the working directory of a process in Compute is a directory in a linked hierarchical file system that is dynamic for each process. In Docker, we can set our working directory by editing the Dockerfile and adding the key WORKDIR

How to get IP address in CentOS

Publish Date:2025/03/23 Views:108 Category:OPERATING SYSTEM

This short article is a brief introduction to CentOS followed by a brief discussion on how we can get the server IP address in CentOS using the Command Line Interface (CLI). This article will discuss some of the commands and their usage for

Updating YUM in Linux

Publish Date:2025/03/23 Views:148 Category:OPERATING SYSTEM

This article will teach us how to update YUM in Linux and how to install, update, remove, find and manage packages on a Linux system. We have also seen the difference yum update between and in Linux yum upgrade . yum update command in Linux

Installing Deb Files in Linux

Publish Date:2025/03/23 Views:93 Category:OPERATING SYSTEM

In this Linux article, we will learn how to install .deb (Debian Package) files on Linux systems. We will also see how to remove .deb files after installation. More importantly, we will learn different ways to install .deb files on Linux sy

lsof Command in Linux

Publish Date:2025/03/23 Views:100 Category:OPERATING SYSTEM

In this Linux article, we will learn about lsof command in Linux operating system. We will see how to use this command for different purposes in Linux. We use lsof the lsof command to verify the ports in use on the Linux operating system. U

ps aux command in Linux

Publish Date:2025/03/23 Views:57 Category:OPERATING SYSTEM

If you are using Linux and are looking for a tool that can monitor all the processes running on your system, then you should use the command ps aux. This command will show you an overview of all running processes. It is very useful for trou

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial