$#foo
, where $foo
is a shell array variable, yields the number of elements in the array $foo
. If $foo
is an ordinary non-array variable, $#foo
yields 1.
csh and tcsh treat shell variables (set by the set
command) quite differently from environment variables (set by the setenv
command or inherited from the parent process). The value of an environment variable is always just a string (which is why $PATH
, for example, needs :
characters to delimit the list elements).
It would probably be more consistent for $#FOO
, where $FOO
is an environment variable, to yield 1
rather than expanding to the value of $FOO
. In any case, the behavior doesn't seem to be documented, and you should avoid relying on it.
If you specifically want the number of directories in your path, $#path
will give you that; $path
is a shell array variable that's automatically tied to the value of the $PATH
environment variable.
For other variables, you just have to keep track of whether a variable is a shell variable or an environment variable.