---
title: "Shell脚本报错unary operator expected"
author: "wlcacc"
date: "2018-08-12T11:02:08Z"
tags: []
categories: ["编程语言"]
slug: "shell-unary-operator-expected"
draft: false
lastmod: "2018-08-12T11:02:08Z"
id: 2428555
url: "https://wlcacc.iteye.com/blog/2428555"
---

在匹配字符串时用了类似这样的语句

 

if\[ $timeofday = "yes"]; then

 

  echo "Good morning"

 

  exit 0

 

报错的原因是：如果变量timeofday的值为空,那么就if语句就变成了if \[  ="yes" ],这不是一个合法的条件。为了避免出现这种情况，我们必须给变量加上引号if \[ "$timeofdat"="yes" ]，这样即使是空变量也提供了合法的测试条件,，if \[  " "="yes"  ]

 

参考：

[Shell脚本报错unary operator expected](https://www.cnblogs.com/Cherie/p/3200294.html)