我正在编写一个Bash脚本,尝试检查是否提供了特定的参数。我注意到
我得到的结果如下:
[ -n arg ]
测试的行为有点奇怪(至少对我来说是这样)。以下是脚本内容:#!/bin/bash
if [ -n $1 ]; then
echo "The 1st argument is of NON ZERO length"
fi
if [ -z $1 ]; then
echo "The 1st argument is of ZERO length"
fi
我得到的结果如下:
with no parameters:
xylodev@ubuntu:~$ ./my-bash-script.sh The 1st argument is of NON ZERO length The 1st argument is of ZERO length
with parameters:
xylodev@ubuntu:~$ ./my-bash-script.sh foobar The 1st argument is of NON ZERO length
$1
用双引号括起来可以得到预期的结果,但我仍然想知道为什么在不使用引号且没有参数调用脚本时两个测试都返回true?看起来此时$1
是null,所以[ -n $1 ]
应该返回false,不是吗?