# 判断shell变量是否空字符串

> 这个错误我也犯过

```bash
#!/bin/sh

STRING=

if [ -z "$STRING" ]; then 
    echo "STRING is empty" 
fi

if [ -n "$STRING" ]; then 
    echo "STRING is not empty" 
fi
```

注意，一定要在变量上加上`""`，表示字符串，然后进行判断。`-z`表示空字符串，`-n`表示非空字符串。

如果忘记加上`""`，则遇到这个逻辑的时候会提示错误

```
line 71: [: too many arguments
```

如果字符串为空，在没有加`""`会导致误判断，导致`-z`和`-n`都成立：

```bash
#!/bin/sh

STRING=

if [ -z $STRING ]; then 
    echo "STRING is empty" 
fi

if [ -n $STRING ]; then 
    echo "STRING is not empty" 
fi
```

执行是错误的结果：

```
STRING is empty 
STRING is not empty
```

## 参考

* [linux shell 中判断字符串为空的正确方法](https://www.cnblogs.com/cute/archive/2011/08/26/2154137.html)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://huataihuang.gitbook.io/cloud-atlas-draft/develop/shell/bash/check_variable_empty.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
