如何比较Bash中的字符串
在编写Bash脚本时,您通常需要比较两个字符串以检查它们是否相等。 当两个字符串长度相同且包含相同的字符序列时,它们是相等的。
本教程描述了如何在Bash中比较字符串。
比较运算符
比较运算符是比较值并返回true或false的运算符。 在Bash中比较字符串时,可以使用以下运算符:
string1 = string2
和string1 == string2
-如果操作数相等,则相等运算符返回true。- 使用
=
运算符test
[
command. - Use the
==
operator with the[[
command for pattern matching.
- 使用
string1 != string2
– The inequality operator returns true if the operands are not equal.string1 =~ regex
– The regex operator returns true if the left operand matches the extended regular expression on the right.string1 > string2
– The greater than operator returns true if the left operand is greater than the right sorted by lexicographical (alphabetical) order.string1 < string2
– The less than operator returns true if the right operand is greater than the right sorted by lexicographical (alphabetical) order.-z string
– True if the string length is zero.-n string
– True if the string length is non-zero.
Following are a few points to be noted when comparing strings:
- A blank space must be used between the binary operator and the operands.
- Always use double quotes around the variable names to avoid any word splitting or globbing issues.
- Bash does not segregate variables by “type”, variables are treated as integer or string depending on the context.
Check if Two Strings are Equal #
In most cases, when comparing strings you would want to check whether the strings are equal or not.
The following script uses the if statement and the test [
command to check if the strings are equal or not with the =
operator:
#!/bin/bash
VAR1="Linuxize"
VAR2="Linuxize"
if [ "$VAR1" = "$VAR2" ]; 然后回显“字符串相等”。 否则,回显“字符串不相等”。 科幻
执行脚本后,它将打印以下输出。
Strings are equal.
这是另一个脚本,它从用户那里获取输入并比较给定的字符串。 在此示例中,我们将使用 [[
命令和 ==
操作员。
#!/bin/bash
read -p "Enter first string: " VAR1
read -p "Enter second string: " VAR2
if [[ "$VAR1" == "$VAR2" ]]; then
echo "Strings are equal."
else
echo "Strings are not equal."
fi
运行脚本并在出现提示时输入字符串:
Enter first string: Linuxize
Enter second string: Ubuntu
Strings are not equal.
您也可以使用逻辑和 &&
和或 ||
比较字符串:
[[ "string1" == "string2" ]] && echo "Equal" || echo "Not equal"
Not equal
检查字符串是否包含子字符串
有多种检查字符串是否包含子字符串的方法。
一种方法是在子字符串周围使用星号符号 *
这意味着匹配所有字符。
#!/bin/bash
VAR='GNU/Linux is an operating system'
if [[ $VAR == *"Linux"* ]]; then
echo "It's there."
fi
该脚本将回显以下内容:
It's there.
另一种选择是使用正则表达式运算符 =~
如下所示:
#!/bin/bash
VAR='GNU/Linux is an operating system'
if [[ $VAR =~ .*Linux.* ]]; then
echo "It's there."
fi
带星号的句点 .*
匹配零个或多个匹配项(换行符除外)的任何字符。
检查字符串是否为空
通常,您还需要检查变量是否为空字符串。 您可以使用 -n
和 -z
操作员。
#!/bin/bash
VAR=''
if [[ -z $VAR ]]; then
echo "String is empty."
fi
String is empty.
#!/bin/bash
VAR='Linuxize'
if [[ -n $VAR ]]; then
echo "String is not empty."
fi
String is not empty.
将字符串与大小写运算符进行比较
除了使用测试运算符,您还可以使用case语句比较字符串:
#!/bin/bash
VAR="Arch Linux"
case $VAR in
"Arch Linux")
echo -n "Linuxize matched"
;;
Fedora | CentOS)
echo -n "Red Hat"
;;
esac
Linuxize matched.
词典比较#
词法比较是通过从左到右依次比较字符串中的字符来按字母顺序比较两个字符串的操作。 这种比较很少使用。
以下脚本按字典顺序比较了两个字符串:
#!/bin/bash
VAR1="Linuxize"
VAR2="Ubuntu"
if [[ "$VAR1" > "$VAR2" ]]; then
echo "${VAR1} is lexicographically greater then ${VAR2}."
elif [[ "$VAR1" < "$VAR2" ]]; then
echo "${VAR2} is lexicographically greater than ${VAR1}."
else
echo "Strings are equal"
fi
该脚本将输出以下内容:
Ubuntu is lexicographically greater than Linuxize.
结论#
比较字符串是Bash脚本中最基本且最常用的操作之一。 阅读本教程后,您应该对如何在Bash中比较字符串有很好的了解。 您还可以查看有关字符串连接的指南。
如果您有任何疑问或反馈,请随时发表评论。
bash终端