C-Shell weirdness (count of words in a variable)

Steve Dunn nose at nbires.UUCP
Wed Mar 26 10:11:40 AEST 1986


Greg Woods writes in response to my article on c-shell weirdness:

> 
> > 2: Count of number of words in a variable (Or when is nothing something)
> > 
> > This:
> > 
> > set hosed = ''
> > echo $#hosed
> > 
> > yields the result '1'
> 
>   But of course! The variable has one value: a null string. And, if you do
> 
> set hosed=( '' junk)
> 
> then what do you suppose $#hosed is? If you got 2, drink a potion of raise
> level and read on! :-) If you do
> 
> set hosed
> echo $#hosed
> 
> THEN you will get 0. The difference? A null string is different from no value
> at all. This too has it's uses, such as when each word of a variable stands
> for an attribute of the object represented by the variable, and a null string
> means that attribute does not exist (i.e. a kludgy implentation of a
> structure in a shell script. And yes, I'm masochistic; I actually have
> real scripts that use this too :-)
> 

Sorry, I tried the example that is supposed to yield zero and I got one
instead. Furthermore, according to the manual page for csh under the set
command

         "The second form [set name] sets name to the null string"

The answer is that you can't have a variable with 0 words at all.
This fact I find counter-intuitive and undocumented
----

The answers Greg gave to my questions on if tests and the not operator 
were correct and very helpful although I still can't figure out where
the documentation says that if a variable name in an if test expands to
one of -r, -w, -x etc, the shell will interpet the expanded variable name
as a command.

                 -Steve "Wrong Way" Dunn



More information about the Comp.bugs.4bsd.ucb-fixes mailing list