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
Splitting a string into variables in Bash
Publish Date:2025/03/23 Views:58 Category:OPERATING SYSTEM
-
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
Bash remove newline from string
Publish Date:2025/03/23 Views:184 Category:OPERATING SYSTEM
-
This article explains how to remove newlines from a string in Bash. Creating a string with newlines in Bash Sometimes, it is required that a string does not have newline characters in it. Bash provides several methods to remove newline char
Remove the first character from a string in Bash
Publish Date:2025/03/23 Views:61 Category:OPERATING SYSTEM
-
Removing the first character of a string can be tricky in Bash because there is no built-in function that can do this directly. However, there are several ways to achieve this, such as using the sed command, the cut command, or substring pa
Replace characters in a string using Bash
Publish Date:2025/03/23 Views:84 Category:OPERATING SYSTEM
-
A common operation involving string literals is replacing individual characters or substrings within the string with other characters or substrings. In this article, we will look at several ways to do string replacement using the BASH shell
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:163 Category:操作系统
-
在本文中,我们将研究使用 BASH shell 进行字符串替换的几种方法。涉及字符串文字的一种常见操作是用其他字符或子字符串替换该字符串中的单个字符或子字符串。
Bash 中的字符串比较运算符
Publish Date:2023/05/18 Views:219 Category:操作系统
-
在本文中,我们将使用 if 语句解释 Bash 中的字符串比较。运行在 Linux 中,提供命令行界面供用户执行不同命令的 shell 程序称为 Bash shell。
在 Bash 中将字符串转换为整数
Publish Date:2023/05/18 Views:168 Category:操作系统
-
本篇文章将讨论 Bash 脚本中的字符串到整数的转换。 首先,我们将讨论字符串的数学运算问题,然后我们将了解如何将字符串转换为整数。