---
title: "shell中判断变量为null"
author: "莫激动"
date: "2012-09-11T20:57:57Z"
tags: []
categories: ["编程语言"]
slug: "shell-null"
draft: false
lastmod: "2012-09-11T20:57:57Z"
id: 1676184
url: "https://mappings.iteye.com/blog/1676184"
---

在shell 中判断变量为null

方法一：

 

```
if [ $x ];then
   echo 'not null'
else
   echo 'is null'
fi
```

 

方法二：

 

```
if [ -z "$x" ];
   echo 'is null'
else
   echo 'not null'
fi
```