检查在bash空字符串 [英] Checking for null string in bash

查看:130
本文介绍了检查在bash空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有下面的测试有何区别?

  [$ STRING=]]&放大器;&安培; 1号出口;
[X $ STRING=X]]&放大器;&安培; 1号出口;
[[-z $ STRING]&放大器;&安培; 1号出口;


解决方案

不,他们都是一样的。但夫妇的防守习惯进入。


  • 您要引用 $ STRING -z 之一,也是

  • 如果您正在使用-u选项(我总是这样),那么你应该引用可能可选变量 $ {运行STRING - } 以防万一它不设置在所有

Is there any difference between the following tests?

[[ "$STRING" = "" ]] && exit 1;


[[ "x$STRING" = "x" ]] && exit 1;


[[ -z $STRING ]] && exit 1;

解决方案

Nope, they are all the same. But couple of defensive habits to get into.

  • You should quote the $STRING in the -z one as well
  • If you are running with the -u option (I always do) then you should reference the possibly optional variable as ${STRING-} just in case its not set at all

这篇关于检查在bash空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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