详尽的文档在info coreutils 'test invocation' man出来的很简略 里头说得很明白
These options test string characteristics. You may need to quote STRING arguments for the shell. For example: test -n "$V" The quotes here prevent the wrong arguments from being passed to `test' if `$V' is empty or contains special characters. 雷锋哪 On Tue, 2011-12-20 at 15:36 +0800, 袁飞 wrote: > 经测试,加了引号就正确了。 > “在中括号内的变量,最好都以双引号括号起来” > 见鸟哥p382 > > 在 2011年12月20日 下午3:22,sanye <xba...@gmail.com>写道: > > > 我怎么记得是 > > if [ -n "$str" ];then > > echo "1" > > fi > > > > if [ -z "$str" ];then > > echo "2" > > fi > > > > 而且,OT了吧 > > On Tue, 2011-12-20 at 15:09 +0800, leon wrote: > > > 在学习bash编程时遇到的问题 > > > 讲解中提示bash脚本中 > > > -n 当字符串不为空时为真 > > > -z 当字符串为空时为真 > > > > > > 那么以下脚本 > > > str="" > > > > > > if [ -n $str ] > > > then > > > echo "1" > > > fi > > > > > > if [ -z $str ] > > > then > > > echo "2" > > > fi > > > > > > 执行时会输出 > > > 1 > > > 2 > > > 为什么会两个都输出呢? > > > str为空同时也不为空?? > > > 还是我理解有误或者设置有误?? > > > ―――― > > > > > 如果说我们是浪漫主义者,是不可救药的理想主义分子,我们想的都是不可能的事情,那么,我们将一千零一次的回答,是的,我们就是这样的人。忠于理想,面对现实。 > > > > > > 若为自由故,需有过墙梯 <http://goo.gl/5liFC> > > > -------------- 下一部分 -------------- > > > 一个HTML附件被移除... > > > URL: < > > https://lists.ubuntu.com/archives/ubuntu-zh/attachments/20111220/ed116556/attachment.html > > > > > > > > > > > -- > > ubuntu-zh mailing list > > ubuntu-zh@lists.ubuntu.com > > https://lists.ubuntu.com/mailman/listinfo/ubuntu-zh > > > -------------- 下一部分 -------------- > 一个HTML附件被移除... > URL: > <https://lists.ubuntu.com/archives/ubuntu-zh/attachments/20111220/bda47c06/attachment.html> -- ubuntu-zh mailing list ubuntu-zh@lists.ubuntu.com https://lists.ubuntu.com/mailman/listinfo/ubuntu-zh