Length of a variable

Hi,
I have another question, I am very new to scripting. I am using c shell.
I am trying to get the length of a variable. Right now I am using
set var = 12
echo $#var

but the # operator isn't working, it is telling me that the length is 1 when it is obviously two....

help!

echo $var | awk '{print length($0)}'

in your case try the one below,

vLEN='echo ${#var}'

Csh considered harmful

From the csh manual page, you'll see that the $#varname construct gives you the number of words contained within the variable, not the number of characters, i.e.

% set blah = `echo "foo bar cog dog"`
% echo $#blah
4

I totally agree with vgersh99 - csh should not be used (wherever possible) for scripting. It is far less powerful and flexible than Bourne-based shells.

For example, with bash

$ foo=12
$ echo ${#foo}
2

Which is exactly the result that you want! :slight_smile:

Cheers
ZB