Operating System - HP-UX
1753878 Members
7118 Online
108809 Solutions
New Discussion юеВ

shell script: how to get the length of a strings

 
SOLVED
Go to solution
mooi-kuan
Advisor

shell script: how to get the length of a strings

Hi Forumist,

var1="testing",

I need to know the length the the var1. How can this be achinved ? ie, will return 7

Thank you.
mk
8 REPLIES 8
Sandor Horvath_2
Valued Contributor

Re: shell script: how to get the length of a strings

Hi !

with awk:
echo $var1 | awk ' { print length($0) } '

regards, Saa
If no problem, don't fixed it.
Sandor Horvath_2
Valued Contributor
Solution

Re: shell script: how to get the length of a strings

Hi !

In posix shell:
${#var1}

regards, saa
If no problem, don't fixed it.
Sandor Horvath_2
Valued Contributor

Re: shell script: how to get the length of a strings

Hi !

In posix shell:
${#var1}

regards, saa
If no problem, don't fixed it.
Sandor Horvath_2
Valued Contributor

Re: shell script: how to get the length of a strings

Hi !

In posix shell:
${#var1}

regards, saa
If no problem, don't fixed it.
Sandor Horvath_2
Valued Contributor

Re: shell script: how to get the length of a strings

Hi !

In posix shell:
${#var1}

regards, saa
If no problem, don't fixed it.
Sandor Horvath_2
Valued Contributor

Re: shell script: how to get the length of a strings

Sorry for many answer, but I get back Java error....

Saa
If no problem, don't fixed it.
Steven Sim Kok Leong
Honored Contributor

Re: shell script: how to get the length of a strings

Hi,

Alternatively,

# echo $var | wc -c

Hope this helps. Regards.

Steven Sim Kok Leong
Brainbench MVP for Unix Admin
http://www.brainbench.com
James R. Ferguson
Acclaimed Contributor

Re: shell script: how to get the length of a strings

Hi:

You can use 'expr' as:

# V=testing
# expr length $V

...JRF...