-1
#!/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?

1 Answers1

1

[[ -n ${numMap[1]} ]] tests if the string is not empty. It isn't, so the test returns true.

muru
  • 72,889
  • thanks, i thought, n stands for the string being null... –  Feb 25 '16 at 09:15
  • 1
    The tests are -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