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...
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 我们还可以使用运算符来测试两个字符串是否不相等!=。 #!/bin/bash...
if [ "$string1" = "$string2" ]; then echo "The two strings are equal." else echo "The two strings are not equal." fi 这是我们执行脚本时的结果: 代码语言:txt AI代码解释 $ ./test.sh The two strings are not equal. 例2 我们还可以使用运算符来测试两个字符串是否不相等!=。 代码语言...
在此示例中,我们使用=运算符和 if 语句来确定两个字符串是否彼此相等。该 if 语句将继续其第一个子句或 else 原因,具体取决于字符串是否相等。 #!/bin/bashstring1="apples"string2="oranges"if["$string1"="$string2"];thenecho"Thetwostringsareequal."elseecho"Thetwostringsarenotequal."fi 这是我们执...
这应该会给你一些在 Bash 中使用字符串的不错的练习。在下一章中,你将学习如何在 Bash 中使用 if-else 语句。敬请关注。 (题图:MJ/aa73b2c9-6d2f-42e2-972d-94fab56d30cc)via: itsfoss.com/bash-string 作者:Abhishek Prakash 选题:lkxed 译者:geekpi 校对:wxy ...
To check if two strings are equal in a Bash script, there are two comparison operators used. First, we’ll discuss the “==” operator. The “==” operator is used to check the equality of two bash strings. In the example below, two strings are defined: strng1 and strng2. Also, ...
The two strings are not equal. 1. 2. 例2 我们还可以使用运算符来测试两个字符串是否不相等!=。 #!/bin/bash string1="apples" string2="oranges" if [ "$string1" != "$string2" ]; then echo "Strings are different." else echo "Strings are not different." ...
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.
if [ "$string1" == "This is my string" ] Let me show it to you with proper examples. Example 1: Check if two strings are equal If you want to check if two strings are equal, here’s an example: #!/bin/bash string1="MyString" string2="MyString" if [ "$string1" == "$...
true if the Strings are equal, case insensitive, or both null In Bash 判断字符串相等 格式1:test "$S1" = "$S2" 格式2:[ "$S1" = "$S2" ] 格式3:test "$S1" == "$S2" 格式4:[ "$S1" == "$S2" ] 格式5:[[ $S1 = $S2 ]] ...