Results 1 to 5 of 5
Hi,
How to find the variable length in Shell.
Ex:
var=string
the 'var' contains "string" of 6 char. so length is 6....
- 02-26-2009 #1Just Joined!
- Join Date
- Aug 2008
- Posts
- 49
Variable length in Shell
Hi,
How to find the variable length in Shell.
Ex:
var=string
the 'var' contains "string" of 6 char. so length is 6.
- 02-26-2009 #2
Feed it to the wc command.
Debian GNU/Linux -- You know you want it.
- 02-26-2009 #3Just Joined!
- Join Date
- Oct 2004
- Posts
- 62
With a little googling on "bash variable length"...
Code:Using the ${#VAR } syntax will calculate the number of characters in a variable.
- 02-26-2009 #4Linux Newbie
- Join Date
- Feb 2009
- Posts
- 99
expr length $string
- 02-26-2009 #5Just Joined!
- Join Date
- Feb 2009
- Posts
- 45
I think itʼs worth noting that your question leaves open an important question: what is your definition of length?
Originally Posted by dayananda.ms
If you are refering the the byte length, then
will give you the right answer, just as according to «GNU-Fan» and «fiomba».Code:$> echo -n "strüng" | wc -c 7 $> expr length "strüng" 7
If your definition of length is equivalent to the amount of intedended characters, then
will give you the expected answer (of course either of both commands depend on the LANG[UAGE] respectively LC_ALL environment variables), as already mentioned by «GNU-Fan» and «signmem».Code:$> echo -n "strüng" | wc -m 6 $> var="strüng"; echo ${#var}; 6


Reply With Quote