Nested quotes in KSH $( ... ) expression



I copied this snippet of Korn Shell script from a message on this group
some time ago. It is supposed to return the path to the directory in
which the executing script is installed.

dir=$(
cmd=$0
[ -e "$cmd" ] || cmd=$(command -v -- "$cmd") || exit
cd -P -- "$(dirname -- "$cmd")" >/dev/null && pwd -P
)

Now that I need to use it, I'm noticing the nested levels of double
quotes (") with the inner pair inside the $( ... ) expression. I don't
see anything that specifically allows this in my Bolsky and Korn book.
Will it work?

If this won't work, is there any other way to get the path to the directory containing $0, assuming that $0 or the path to it may contain spaces?
--
Norm

To reply, change domain to an adult feline.

.