在bash未设置空变量之间的区别 [英] difference between unset and empty variables in bash

查看:128
本文介绍了在bash未设置空变量之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用bash,什么是检查一个变量是空的或不是最好的方法是什么?

如果我使用:

 如果[-z$ VAR]

的建议在一个论坛上这个工程的一个未设置变量但是当变量被设置为空,但它是真实的。
建议?


解决方案

 如果[`设置| grep'可以^ VAR = $'`]

本搜索中设置的变量列表中的字符串VAR =

Using bash, what's the best method to check if a variable is empty or not?

If I use:

if [ -z "$VAR" ]

as suggested in a forum this works for an unset variable but it is true when the variable is set but empty. Suggestions?

解决方案

if [ `set | grep '^VAR=$'` ]

This searches for the string "VAR=" in the list of variables set.

这篇关于在bash未设置空变量之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆