时间:2025-01-26 18:01:40
shell脚本判断字符串相等
在shell脚本中,可以使用`=`或`==`来判断两个字符串是否相等。例如:
```shell
str1 = "hello"
str2 = "world"
if [ "$str1" =="$str2" ] ; then
echo "两个字符串相等"
else
echo "两个字符串不相等"
fi
```
需要注意的是,判断字符串相等时,两个字符串之间要用引号括起来,否则会报错。
另外,还可以使用`if [ \"$test\"x = \"test\"x ]]`来比较两个字符串是否相等,其中`x`是为了防止当`$test`为空时,表达式变成`x = testxtestx`,导致不相等。
快测评广州东远堂信息科技有限公司版权所有 网站地图提供支持 粤ICP备15011623号