shell判断字符串相等
时间: 2023-04-28 15:05:59 浏览: 74
在 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中,可以使用`[ ]`或`test`命令来判断字符串是否相等。例如:
```
if [ "$str1" = "$str2" ]; then
echo "str1 equals str2"
fi
# 或者
if test "$str1" = "$str2"; then
echo "str1 equals str2"
fi
```
需要注意的是,`=`两边的字符串需要用双引号括起来,否则如果字符串为空会报错。如果要判断字符串不相等,则可以使用`!=`操作符。
shell 脚本判断字符串相等
在 Shell 脚本中,可以使用 `=` 或 `==` 来判断两个字符串是否相等。例如:
```shell
str1="hello"
str2="world"
if [ "$str1" = "$str2" ]; then
echo "两个字符串相等"
else
echo "两个字符串不相等"
fi
```
其中,`=` 和 `==` 的作用是相同的,都是用来判断两个字符串是否相等。需要注意的是,判断字符串相等时,两个字符串之间要用引号括起来,否则会报错。