哪个`[`不能比较空字符串? [英] Which `[` can not compare empty strings?

查看:33
本文介绍了哪个`[`不能比较空字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一些古老的 Solaris 的 Shell 脚本中,我发现了以下符号来检查变量是否为空:

In some ancient Shell scripts for Solaris I found the following notation to check if a variable is empty:

[ x"$var" = x ]

在当前的 Linux 系统上,我会像这样编写

On a current Linux system I would write it just as

[ "$var" = "" ]

[ -z "$var" ]

哪个版本的 [ 实际上需要第一个符号?

Which version of [ actually requires the first notation?

推荐答案

None.the alias from [] to test 出现在 test 的选项 -z 之后.因此,如果你可以使用[ x"$var" = x],你也可以写[ -z "$var" ].

None.The alias from [] to test appears after the option -z of test. Therefore, if you can use [ x"$var" = x], you can also write [ -z "$var" ].

这篇关于哪个`[`不能比较空字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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