Differences between revisions 9 and 16 (spanning 7 versions)
Revision 9 as of 2011-05-26 13:28:27
Size: 1280
Editor: sbl-eh4-rp1
Comment:
Revision 16 as of 2015-03-05 00:24:26
Size: 1522
Editor: izabera
Comment: syntax hl
Deletions are marked like this. Additions are marked like this.
Line 5: Line 5:
{{{ {{{#!highlight bash
Line 7: Line 7:
${#varname} "${#varname}"
Line 9: Line 9:

(note that with `bash` 3 and above, that's the number of characters, not bytes, which is a significant differences in multi-byte locales. Behaviour of other shells in that regard vary).
Line 12: Line 14:
{{{ {{{#!highlight bash
Line 14: Line 16:
expr "$varname" : '.*' expr "x$varname" : '.*' - 1
Line 17: Line 19:
({{{expr}}} prints the number of characters matching the pattern {{{.*}}}, which is the length of the string.) ({{{expr}}} prints the number of characters or bytes matching the pattern {{{.*}}}, which is the length of the string (in bytes for GNU `expr`). The `x` is necessary to avoid problems with `$varname` values that are `expr` operators)
Line 21: Line 23:
{{{ {{{#!highlight bash
Line 23: Line 25:
expr length "$varname" expr length "x$varname" - 1
Line 28: Line 30:
This second version is not specified in [[POSIX]], so is not portable across all platforms.  However, if {{{$varname}}} expands to {{{"length"}}}, the first version will fail with BSD/GNU {{{expr}}}.

A portable way is:

{{{
expr \( "X$varname" : ".*" \) - 1
}}}
This second version is not specified in [[POSIX]], so is not portable across all platforms.
Line 38: Line 34:
{{{
# Bourne
awk -v x="$varname" 'BEGIN {print length(x)}'
{{{#!highlight bash
# Bourne with POSIX awk
awk 'BEGIN {print length(ARGV[1])}' "$varname"
Line 43: Line 39:
Though that one will fail for values of $varname that contain backslash characters, so you may prefer:

{{{
# Bourne with POSIX awk
awk 'BEGIN {print length(ARGV[1]);exit}' "$varname"
}}}
(there, whether the length is expressed in bytes or characters depends on the implementation (for instance, it's ''characters'' for GNU awk, but ''bytes'' for `mawk`).
Line 54: Line 44:
{{{ {{{#!highlight bash
Line 56: Line 46:
${#arrayname[@]} "${#arrayname[@]}"
Line 59: Line 49:
Returns the number of elements in an array. Expands to the number of elements in an array.
Line 61: Line 51:
{{{ {{{#!highlight bash
Line 63: Line 53:
${#arrayname[i]} "${#arrayname[i]}"
Line 66: Line 56:
Returns the length of the array's element i. Expands to the length of the array's element i.

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

The fastest way, not requiring external programs (but not usable in Bourne shells):

   1 # POSIX
   2 "${#varname}"

(note that with bash 3 and above, that's the number of characters, not bytes, which is a significant differences in multi-byte locales. Behaviour of other shells in that regard vary).

or for Bourne shells:

   1 # Bourne
   2 expr "x$varname" : '.*' - 1

(expr prints the number of characters or bytes matching the pattern .*, which is the length of the string (in bytes for GNU expr). The x is necessary to avoid problems with $varname values that are expr operators)

or:

   1 # Bourne, with GNU expr(1)
   2 expr length "x$varname" - 1

(BSD/GNU expr only)

This second version is not specified in POSIX, so is not portable across all platforms.

One may also use awk:

   1 # Bourne with POSIX awk
   2 awk  'BEGIN {print length(ARGV[1])}' "$varname"

(there, whether the length is expressed in bytes or characters depends on the implementation (for instance, it's characters for GNU awk, but bytes for mawk).


Similar needs:

   1 # Korn/Bash
   2 "${#arrayname[@]}"

Expands to the number of elements in an array.

   1 # Korn/Bash
   2 "${#arrayname[i]}"

Expands to the length of the array's element i.


CategoryShell

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