How do I count # of char. in a word?

I havent done shell scripting in quite some time. I want to know how to count the number of characters in a word, specifically a parameter.

Example: myscript hello

I want "myscript" to return the number of charcaters in the parameter "hello".

Any ideas?

I suppose there's several ways, but one is awk:

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

thanks oombera. I might use this but I'm helping a friend write a script for an Inro. to Unix class and the class doesnt cover the awk command. Could you list any other ways that you know?

and thanks for your help

The length of a parameter is ${#parameter}

please read the rules. no homework questions.

the class might not cover a command but you can bet your $$ that the teacher covered the material needed in class or as a reading assignment.

man wc.

Just as a sidenote, I thought of wc first, but when I used it, it returned one greater than the number of characters in each string it tested, so that "hello" would return "6"... is it counting some end-of-line character?

to tell you the truth i am unsure what the 6th byte is. never noticed it before.

the only thing i can think of is this snipit from the environ(5) man page.

anyone else have any insite into why wc counts more 1 more charicter or byte then is in the file?

           LC_CTYPE
                 This category  specifies  character  classifica-
                 tion, character conversion, and widths of multi-
                 byte characters. When   LC_CTYPE  is  set  to  a
                 valid value, the calling utility can display and
                 handle text  and  file  names  containing  valid
                 characters for that locale;   Extended Unix Code
                 (EUC) characters where any individual  character
                 can be 1, 2, or 3 bytes wide; and EUC characters
                 of 1, 2, or 3 column  widths.  The  default  "C"
                 locale  corresponds to the 7-bit ASCII character
                 set; only characters from ISO 8859-1 are  valid.
                 The  information  corresponding to this category
                 is  stored  in  a  database   created   by   the
                 localedef()  command.  This environment variable
                 is used by ctype(3C), mblen(3C), and  many  com-
                 mands, such as cat(1), ed(1), ls(1), and vi(1).

I think that it's easier to explain with a demo...

$ echo hello|wc -c
6

$ echo hello|od -hc
0000000 6865 6c6c 6f0a
h e l l o \n
0000006

$ printf hello|wc -c
5

$ printf hello|od -hc
0000000 6865 6c6c 6f00
h e l l o
0000005

didnt know echo put a NEWLINE char at the end.

but also when i put hello in a text file useing vi i also get 6 char.
that octal dump program looks interesting. will deffinetly check out that man page.

Okay, that's understandable, Ygor. From the man page for echo:

From the man page for wc:

So basically, the newline character is there and wc has no way of suppressing it itself, so it must be suppressed with printf, echo -n, or something similar...

Thanks Ygor!