else echo "Strings are not equal." fi 2. 使用 tr 命令去除所有空白字符 代码语言:txt 复制 string1=$(echo "your_string" | tr -d '[:space:]') string2=$(echo "your_string" | tr -d '[:space:]') if [ "$string1" == "$string2" ]; then echo "Strings are equal." else echo...
str1="Hello" str2="World" if [ "$str1" != "$str2" ]; then echo "Strings are not equal" else echo "Strings are equal" fi 字符串为空比较:可以使用双等号(==)来比较一个字符串是否为空。例如: 代码语言:txt 复制 str="" if [ "$str" == "" ]; then echo "String is empty" els...
String1 and String2 are equal. String1 and String3 are not equal. 这里,如果我们先用 = 运算符比较 String1 和 String2。由于 String1 和 String2 都具有相同的长度,具有相同的字符序列,比较运算符返回 true,因此我们得到 String1 and String2 are equal.. 作为程序中第一个 if-else 块的输出。 同样...
在bash中,\n并不等于'n'。在你的示例中,出现Strings are equal.的输出,是因为在bash字符串比较中的特殊行为,但这并不是因为\n被解释为'n'。 实际上,当你在bash中设置变量str1=\n时,你并没有正确地包含一个换行符(\n),而是仅仅设置了一个名为str1的变量,其值为n(因为反斜杠\在这种情况下没有被正确...
string2="oranges" if [ "$string1" = "$string2" ]; then echo "The two strings are equal." else echo "The two strings are not equal." fi 这是我们执行脚本时的结果: $ ./test.sh The two strings are not equal. 例2 我们还可以使用运算符来测试两个字符串是否不相等!=。
The two strings are not equal. 1. 2. 例2 我们还可以使用运算符来测试两个字符串是否不相等!=。 AI检测代码解析 #!/bin/bash string1="apples" string2="oranges" if [ "$string1" != "$string2" ]; then echo "Strings are different." ...
$./test.shThetwostringsarenotequal. 例2 我们还可以使用运算符来测试两个字符串是否不相等!=。 #!/bin/bashstring1="apples"string2="oranges"if["$string1"!="$string2"];thenecho"Stringsaredifferent."elseecho"Stringsarenotdifferent."fi
Checking if two Bash script strings are equal isn’t a big thing per se, and even if the Bash script strings are not equal, that’s not also a big thing. This article explains everything you need to know about Bash string equality, plus how you can access and use Linux files on Win...
-eq 两数值相等 (equal) -ne 两数值不等 (not equal) -gt n1 大于 n2 (greater than) -lt n1 小于 n2 (less than) -ge n1 大于等于 n2 (greater than or equal) -le n1 小于等于 n2 (less than or equal) 5. 判定字符串的数据 test -z string 判定字符串是否为 0 ?若 string 为空字符串,...
如果您更改任何字符串,您将得到Not equal,您可以在这里尝试。 bash中一个字符串中的多个正则表达式匹配 有关纯bash解决方案,请参见以下示例。 Example script: # cat foo.shshopt -s extglobvar='[12 chapters][13 chapters][14 chapters]'while [[ $var =~ ([0-9]+ chapters) ]]; do echo "${...