The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
help need to find the length of a variable smr_rashmy Shell Programming and Scripting 7 02-18-2008 03:02 AM
use awk to read variable length csv shweta_d Shell Programming and Scripting 3 06-17-2007 10:16 PM
Length of an indirect variable gone_bush Shell Programming and Scripting 0 03-28-2006 04:17 PM
Set length for System Variable bryanthomas Shell Programming and Scripting 2 01-12-2006 11:32 AM
creating a fixed length output from a variable length input r1500 Shell Programming and Scripting 2 12-03-2003 10:09 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 02-08-2005
Registered User
 

Join Date: Feb 2005
Posts: 10
Unhappy 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!
Reply With Quote
Forum Sponsor
  #2  
Old 02-08-2005
Registered User
 

Join Date: Jan 2005
Posts: 97
Quote:
Originally Posted by karyn1617
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}'

Last edited by thumsup9; 02-08-2005 at 12:46 PM.
Reply With Quote
  #3  
Old 02-08-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,003
Quote:
$#name
${#name} These give the number of words in the variable.
Csh considered harmful
Reply With Quote
  #4  
Old 02-08-2005
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Quote:
Originally Posted by karyn1617
but the # operator isn't working, it is telling me that the length is 1 when it is obviously two....
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.
Code:
% 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
Code:
$ foo=12
$ echo ${#foo}
2
Which is exactly the result that you want!

Cheers
ZB
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:04 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0