shell 判断字符串相等
时间: 2023-11-22 10:05:07 浏览: 30
在 shell 中,可以使用 `=` 或 `==` 运算符来判断两个字符串是否相等。例如:
```
if [ "$str1" = "$str2" ]; then
echo "字符串相等"
else
echo "字符串不相等"
fi
```
其中,`$str1` 和 `$str2` 分别是要比较的两个字符串。需要注意的是,在使用 `=` 或 `==` 进行字符串比较时,两边的字符串需要加上双引号,以避免出现空格等特殊字符导致比较失败的情况。
相关问题
shell判断字符串相等
在 shell 中判断字符串相等可以使用 "==" 或 "="。例如:
```
if [ "$string1" == "$string2" ]; then
echo "The strings are equal."
else
echo "The strings are not equal."
fi
```
或者
```
if [ "$string1" = "$string2" ]; then
echo "The strings are equal."
else
echo "The strings are not equal."
fi
```
如果判断相等就可以使用 "==" 和 "=" 两种方式是等效的。
shell 脚本判断字符串相等
在 Shell 脚本中,可以使用 `=` 或 `==` 来判断两个字符串是否相等。例如:
```shell
str1="hello"
str2="world"
if [ "$str1" = "$str2" ]; then
echo "两个字符串相等"
else
echo "两个字符串不相等"
fi
```
其中,`=` 和 `==` 的作用是相同的,都是用来判断两个字符串是否相等。需要注意的是,判断字符串相等时,两个字符串之间要用引号括起来,否则会报错。