美文网首页
[bash]字符串判空有空格报错:binary operator

[bash]字符串判空有空格报错:binary operator

作者: SQUA2E | 来源:发表于2019-07-29 10:45 被阅读0次

最近写代码时,遇到了binary operator expected报错,代码类似如下

filename="hello world"
if [ -e $filename ]; then
    echo $filename
else
    echo "nothing"
fi

查阅得知,在使用-e,-n,-d等作为if的条件判断时 如果参数$filename本身包含空格,则会报错。
解决放法:使用[[ -e $filename]] 或者 [ -e "$filename" ]

相关文章

网友评论

      本文标题:[bash]字符串判空有空格报错:binary operator

      本文链接:https://www.haomeiwen.com/subject/gavvrctx.html