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...
if [ "$str1" != "$str2" ]; then echo "Strings are not equal" else echo "Strings are equal" fi 字符串为空比较:可以使用双等号(==)来比较一个字符串是否为空。例如: 代码语言:txt 复制 str="" if [ "$str" == "" ]; then echo "String is empty" else echo "String is not empty"...
#!/bin/bash # 示例1:使用 = 进行字符串相等比较 string1="hello" string2="hello" if [ "$string1" = "$string2" ]; then echo "The strings are equal." else echo "The strings are not equal." fi # 示例2:使用 != 进行字符串不相等比较 string1="hello" string2="world" if [ "$stri...
if [[ "$String1" != "$String3" ]]; then echo "String1 and String3 are not equal." else echo "String1 and String3 are equal." fi 输出: String1 and String2 are equal. String1 and String2 are equal. String1 and String3 are not equal. 这里,如果我们先用 = 运算符比较 String1 ...
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 我们还可以使用运算符来测试两个字符串是否不相等!=。
如果相等,则输出"Strings are equal",否则输出"Strings are not equal"。 接下来,让我们来看看如何在Bash中使用正则表达式进行字符串匹配。我们可以使用"=~"符号来进行正则表达式匹配,例如: bash. #!/bin/bash. str="hello world" if [[ $str =~ [0-9] ]]; then. echo "String contains a number" ...
$./test.shThetwostringsarenotequal. 例2 我们还可以使用运算符来测试两个字符串是否不相等!=。 #!/bin/bashstring1="apples"string2="oranges"if["$string1"!="$string2"];thenecho"Stringsaredifferent."elseecho"Stringsarenotdifferent."fi
if [ "$str1" = "$str2" ]; then echo "Strings are equal." else echo "Strings are not equal." fi 输出结果 Strings are equal. 在bash中为何\n等于'n'? bash 收藏 BETA 在bash中,\n并不等于'n'。在你的示例中,出现Strings are equal.的输出,是因为在bash字符串比较中的特殊行为,但这并不...
[ STRING1 > STRING2 ] 如果 “STRING1” sorts after “STRING2” lexicographically in the current locale则为真。 [ ARG1 OP ARG2 ] “OP” is one of -eq, -ne, -lt, -le, -gt or -ge. These arithmetic binary operators return true if “ARG1” is equal to, not equal to, less than...
echo "Strings are not equal." fi if [ "${string1}" != "${string2}" ]; then echo "Strings are different." else echo "Strings are the same." fi 参考链接 Bash String Comparison Bash Conditional Expressions 通过以上信息,您应该能够理解 Bash 中字符串比较运算符的基础概念、优势、类型、应用...