Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to print the length of a variable 1

Status
Not open for further replies.

tg2003

IS-IT--Management
Feb 6, 2003
270
IL
using tcsh and sed/awk etc.

thanks!
 
Two other ways:

Code:
#!/bin/ksh

str="1234x"
len=$(expr "$str" : '.*')
echo $len
[code]

Another way:

[code]
#!/bin/ksh

str="1234x"
len=${#str}
echo $len
 

Sorry, I'll get it right ...

Code:
#!/bin/ksh

str="1234x"
len=$(expr "$str" : '.*')
echo $len

Code:
#!/bin/ksh

str="1234x"
len=${#str}
echo $len
 
sorry if I'm nitpicking, but

echo $JAVA_HOME | wc -c

will squeeze multiple whitespace into just 1 space (not an issue for this variable but still...) and will add one newline char to the count.

echo "${JAVA_HOME}\c"|wc -c
or
echo -n "${JAVA_HOME}|wc -c

depending on how your echo works, seem more correct to me


HTH,

p5wizard
 
Yes, echo -n should be used, and few variables contain whitespaces:
Code:
echo $PROMPT_COMMAND | sed 's/[ \t]/_/g' | wc -c

If you have a postgresql database running, containing a database 'kram', containing a table 'dual', you could use:
Code:
psql -c "SELECT distinct length('$JAVA_HOME') from dual" kram
:)

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top