shell 判断字符串相等
时间: 2023-04-14 15:04:45 浏览: 123
在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
```
其中,`=` 和 `==` 的作用是相同的,都是用来判断两个字符串是否相等。需要注意的是,判断字符串相等时,两个字符串之间要用引号括起来,否则会报错。