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" ]; the
else echo "Strings are not equal as expected." fi 测试脚本 将上述脚本保存到一个文件中,比如compare_strings.sh,然后赋予执行权限并运行它: bash chmod +x compare_strings.sh ./compare_strings.sh 你应该会看到输出表明str1和str2是相等的,而str1和str3是不相等的。 总结 Bash中判断字符串相等的方法...
/bin/bashread-p"Enter first string: "VAR1read-p"Enter second string: "VAR2if[["$VAR1"=="$VAR2"]];thenecho"Strings are equal."elseecho"Strings are not equal."fi 您还可以使用逻辑和&&和或||比较字符串: [["string1"=="string2"]]&&echo"Equal"||echo"Not equal" 如果你需要检查字符...
Example 2: Check if strings are not equal in Bash Instead of checking the quality, let’s do the opposite and check the inequality. Bash also provides the negation operator so that you can easily use “if not equal” condition in shell scripts. if [ "$string1" != "Not MyString" ] ...
String1 and String2 are equal.String1 and String2 are equal.String1 and String3 are not equal. 这里,如果我们先用=运算符比较String1和String2。由于String1和String2都具有相同的长度,具有相同的字符序列,比较运算符返回true,因此我们得到String1 and String2 are equal.作为程序中第一个if-else块的输出...
Compare Strings Using the Not Equal Operator -ne in Bash As mentioned, we will use != to compare the strings. Let’s look at an example. #!/bin/bash nameone="Bobby" nametwo="Shera" if [[ $nameone != $nametwo ]]; then echo "Not Equal!" else echo "Equal!" fi We declared ...
When writing Bash scripts you will often need to compare two strings to check if they are equal or not. Two strings are equal when they have the same length and contain the same sequence of characters. This tutorial describes how to compare strings in Bash....
It is advisable always to check and compare if two strings are equal in a Bash script; this is quite important for different reasons. If two strings are equal in a Bash script, it implies that both strings have the same length and character sequence. The “if” statement is used to chec...
To compare strings in the shell scripting the string comparison operators=,==,!=are used. Let’s create a Bash script to take the username as input from the user and compare the name string with the hardcoded name. #!/bin/bash
Check file types and compare values. Returns 0ifthe condition evaluates totrue, 1ifit evaluates tofalse. More information: https://www.gnu.org/software/bash/manual/bash.html#index-_005b_005b. - Testifa given variable is equal/not equal to the specified string: ...