#!/bin/bash
declare -A numMap
numMap[1]=1
#case-one
if ! [[ ${numMap[1]} ]];then
echo "case-one: the key 1 for numMap array is not set"
fi
#case-two
if [[ -n ${numMap[1]} ]]; then
echo "case-two: the key 1 for numMap array is not set"
fi
I expect both ! [[ ${numMap[1]} ]]
and [[ -n ${numMap[1]} ]]
to be falsy, since numMap[1]
contains a valid value - 1
. But when i run the code, it prints
case-two: the key 1 for numMap array is not set
Why does, [[ -n ${numMap[1]} ]]
get evaluated as true?
n
stands for the string beingnull
... – Feb 25 '16 at 09:15-n
and-z
so you can think of them as testing if the string length is non-zero or zero. – muru Feb 25 '16 at 09:16