I'm checking the following code:
#!/usr/bin/env bash
set -o errexit
set -o nounset
if [ -n "$@" ]; then
echo "not null";
else
echo "null";
fi
This means that if I call my script like so ./my-script.sh parameter
it should result with not null
and when called like so ./my-script.sh
it should say null
.
Documentation says that -z
means string is null, that is, has zero length
and -n
means that string is not null
.
For some reason my code always claims that $@
is not null.
Why is that?
When I replace -n
with -z
and swap the content like so:
if [ -z "$@" ]; then
echo "null";
else
echo "not null";
fi
then it works correctly.