String comparison operators in Bash
In this article, we will explain string comparison in Bash using if statement.
The shell program that runs in Linux and provides a command line interface for users to execute different commands is called Bash shell. It is also used as the default shell in many Linux distributions and is called GNU Bourne-Again Shell (Bash).
Bash scripts
A series of Bash commands written in a file is called a Bash script. The Bash shell executes these commands after reading the file.
The file extension for a Bash script is .sh. The following contents of a file named First.sh are shown below.
#!/bin/Bash
echo "Hello World"
The above file First.sh is a Bash script that contains only one echo command, which displays Hello world on the terminal.
#!/bin/Bash
The first line of the file tells the Bash program on your system to act as an interpreter and run the commands written in the script file.
Bash shell provides different methods to execute scripts on the terminal. Some of the methods are discussed below.
-
The bash command followed by a Bash script file is used to execute the script. The following command runs the First.sh script.
The output of this command is:bash First.sh
Hello World
-
Run the Bash script by specifying the path.
chmod +x First.sh
After giving the script file executable permissions using , we can run the script file by specifying the path of the script.
We can run the script using the above method using the absolute path or using the relative path of the script ./First.sh.<path to the script file>/First.sh
String variables in Bash
We can use the assignment operator (=) in Bash scripts to declare and initialize any string in a variable.
For example:
#!/bin/Bash
S="Hello World"
echo $S
In the above example, we have declared a string variable S and initialized it with Hello World as the value. The echo command uses the echo command and the $ operator to display the value of the string variable on the terminal.
String comparison operators in Bash scripting
We can use = (equal to) operator in Bash script to compare two strings. We also use ==
operator to compare strings.
==
Is a synonym for the = operator for string comparisons.
For example, consider a Bash script First.sh with the following contents.
#!/bin/Bash
S1="Hello World"
S2="Hello World"
if [ "$S1" = "$S2" ]
then
echo "Equal"
else
echo "Not Equal"
fi
The following script contains two strings, S1 and S2, that have the same value. The if condition uses the = operator to compare the strings; however, we can also use if [ "$S1" == "$S2" ]
the statement to compare these strings.
Following is the First.sh script output.
Equal
!=
The (not equal to) operator is used to match if two strings are not equal. We can use if [ "$S1" != "$S2" ]
this operator in if statements.
This statement returns true if the strings S1 and S2 are not equal.
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.
Related Articles
Convert a string to an integer in Bash
Publish Date:2025/03/23 Views:154 Category:OPERATING SYSTEM
-
This article will discuss string to integer conversion in Bash script. First, we will discuss the math operations on strings and then we will see how to convert a string to an integer. Mathematical operations on strings Let's start our disc
Get the length of a string in Bash
Publish Date:2025/03/23 Views:145 Category:OPERATING SYSTEM
-
In programming languages, the length or size of a data type plays an important role. It helps in list traversal and facilitates the extraction of useful information. The length is especially important when performing tasks that require trav
使用 Bash 替换字符串中的字符
Publish Date:2023/05/18 Views:159 Category:操作系统
-
在本文中,我们将研究使用 BASH shell 进行字符串替换的几种方法。涉及字符串文字的一种常见操作是用其他字符或子字符串替换该字符串中的单个字符或子字符串。
Bash 中的字符串比较运算符
Publish Date:2023/05/18 Views:218 Category:操作系统
-
在本文中,我们将使用 if 语句解释 Bash 中的字符串比较。运行在 Linux 中,提供命令行界面供用户执行不同命令的 shell 程序称为 Bash shell。
在 Bash 中将字符串转换为整数
Publish Date:2023/05/18 Views:167 Category:操作系统
-
本篇文章将讨论 Bash 脚本中的字符串到整数的转换。 首先,我们将讨论字符串的数学运算问题,然后我们将了解如何将字符串转换为整数。
在 Bash 中获取字符串的长度
Publish Date:2023/05/18 Views:299 Category:操作系统
-
字符串是不同字符的序列,其中可能包含空格。 在 Bash 中,字符串的长度是该字符串中字符的总数。
在 Bash 中将字符串拆分为变量
Publish Date:2023/05/18 Views:211 Category:操作系统
-
本篇文章将讨论在 Bash 中将字符串拆分为变量的不同方法。我们将从对字符串的简要介绍开始我们的讨论。 Bash 中的字符串 字符串是字符的组合/集合。 在 Bash 中,字符串是一种类似于整数或浮
Bash 从字符串中删除换行符
Publish Date:2023/05/18 Views:239 Category:操作系统
-
本篇文章介绍如何在 Bash 中从字符串中删除换行符。在 Bash 中创建一个带有换行符的字符串 有时,要求字符串中不要有换行符。
从 Bash 中的字符串中删除第一个字符
Publish Date:2023/05/18 Views:261 Category:操作系统
-
这篇文章将回顾如何使用每种方法从 Bash 中的字符串中删除初始字符。在 Bash 中使用 sed 命令从字符串中删除第一个字符