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

maartenj at ark.UUCP maartenj at ark.UUCP
Sun Mar 30 10:22:59 AEST 1986


In article <2024 at hao.UUCP> woods at hao.UUCP writes:
>> > set hosed
>> > echo $#hosed
>> > 
>> > THEN you will get 0.
>> 
>> Sorry, I tried the example that is supposed to yield zero and I got one
>> instead.
>> 
>> The answer is that you can't have a variable with 0 words at all.
>> This fact I find counter-intuitive and undocumented
>
>    I stand corrected. It seems that $#var is either an error or a positive
>integer. Counterintuitive is debatable, however; a variable with no words
>is kind of like saying something that is nothing. The only exception
>I am aware of to this is that $#argv CAN be zero if it occurs in a shell
>which was given no arguments (which includes your login shell; try it).
>THIS is what is counter-intutive to ME, that argv behaves differently than
>all other variables in this respect.

Try :
	set hosed = ()
	echo $#hosed 
This will give you 0.

The point is the definitions of a `word'. A word is defined as being separated
by a tab, space or newline accept when the string, containing these characters,
is enclosed in `'' or  `"' (with one exception when you do something like:
"`cat /etc/passwd`" this will force new words at newlines).

The actual syntax is of `set' is :
	set arg = ( wordlist )
when you only set the arg to one word you can leave out the `(' and `)':
	set arg = word
Finally you have the abbreviation :
	set arg
This is exactly the same as
	set arg = ''
e.i. it will set arg to a null string.

So $#arg will give the number of words in $arg.
-- 

			Maarten Jan  Huisjes.  (maartenj at vu44.UUCP)
			{seismo|decvax|philabs}!mcvax!vu44!maartenj



More information about the Comp.unix.wizards mailing list