Shellscripting -z option

Can someone explain the -z option in shellscripting.

I did this. When I was in both ksh and bash it echoed back hi.

#!/bin/ksh

if [ -z "$BASH_VERSION" ]
then
    echo hi

fi

When I echoed $BASH_VERSION in bash it gave a version. When I echoed $BASH_VERSION in ksh it was blank.

I thought -z was only true if the length of the string is 0? This is what my man page says.

-z string = True if the length of the string is 0. 
echo ${#BASH_VERSION}

gives the length of a variable, i.e., the number of characters, whether numeric or not numeric.

I do not see what is going on.

Try:

#!/bin/bash
echo "bash version = $BASH_VERSION, length = ${BASH_VERSION}"

plus, you show ksh as the shell being run.

Please post the output - this way we KNOW what shell is running.

Is this what you wanted to see?

bash-4.2$ echo $BASH_VERSION
4.2.50(1)-release
bash-4.2$ ksh
$ echo $BASH_VERSION

$

--- Post updated at 05:42 PM ---

I think this is what you wanted.

bash-4.2$ echo ${#BASH_VERSION}
17
bash-4.2$ ksh
$ echo ${#BASH_VERSION}
0
$

Your script as originally posted showed a shebang for ksh.
Here is what I get:

$ cat t.shl
#!/bin/bash

if [ -z "$BASH_VERSION" ]
then
    echo hi
else
    echo "characters found"
fi

Owner@Owner-PC ~
$ ./t.shl
characters found

Next, I have dash not ksh so:

$ cat t.shl
#!/bin/dash

if [ -z "$BASH_VERSION" ]
then
    echo hi
else
    echo "characters found"
fi

Owner@Owner-PC ~
$ ./t.shl
hi

Both work as expected. So I cannot duplicate your problem. Which means to me you did not use shebang invocation and/or you got mixed up results you could not interpret. There is no other answer ...that I can see.

Perhaps you think that your script checks your current shell. That is not the case.
The script is run by a new shell and reports on that shell.
If you run the script with its scriptname then it reports on the shell given in the #! shebang.
If you run the script with bash scriptname it finds bash.
If you run the script with ksh scriptname it finds ksh.
To report on the current (calling) shell the only solution is to include/source it:

. scriptname