Differences between revisions 3 and 5 (spanning 2 versions)
Revision 3 as of 2008-11-22 21:07:00
Size: 524
Editor: GreyCat
Comment: first-line; also a bit more explicit wording
Revision 5 as of 2009-12-29 17:23:36
Size: 701
Editor: MatthiasPopp
Comment:
Deletions are marked like this. Additions are marked like this.
Line 24: Line 24:

{{{
${#arrayname[@]}
}}}

Returns the Number of Elements in an Array.

{{{
${#arrayname[i]}
}}}

Returns the length of the Arrays Element i.

----
CategoryShell

Is there a function to return the length of a string?

The fastest way, not requiring external programs (but usable only with BASH and KornShell):

${#varname}

or for Bourne shells:

expr "$varname" : '.*'

(expr prints the number of characters matching the pattern .*, which is the length of the string.)

Another one:

expr length "$varname"

(This is for a BSD/GNU version of expr. Do not use this, because it is not POSIX).

${#arrayname[@]}

Returns the Number of Elements in an Array.

${#arrayname[i]}

Returns the length of the Arrays Element i.


CategoryShell

BashFAQ/007 (last edited 2015-03-05 00:24:26 by izabera)