Differences between revisions 2 and 3
Revision 2 as of 2008-11-22 14:08:37
Size: 484
Editor: localhost
Comment: converted to 1.6 markup
Revision 3 as of 2008-11-22 21:07:00
Size: 524
Editor: GreyCat
Comment: first-line; also a bit more explicit wording
Deletions are marked like this. Additions are marked like this.
Line 4: Line 4:
Line 8: Line 9:
or or for Bourne shells:
Line 14: Line 15:
({{{expr}}} prints the number of characters matching the pattern {{{.*}}}, which is the length of the string) ({{{expr}}} prints the number of characters matching the pattern {{{.*}}}, which is the length of the string.)
Line 16: Line 17:
or Another one:
Line 22: Line 23:
(for a BSD/GNU version of {{{expr}}}. Do not use this, because it is not [[POSIX]]). (This is for a BSD/GNU version of {{{expr}}}. Do not use this, because it is not [[POSIX]]).

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).

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