Differences between revisions 4 and 14 (spanning 10 versions)
Revision 4 as of 2009-06-09 13:19:16
Size: 678
Editor: localhost
Comment: its about length.. it should also bring examples for arrays
Revision 14 as of 2015-01-14 12:24:43
Size: 1495
Comment: remove broken code for simplification. exit only needed in old awk (which doesn't support ARGV)
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
The fastest way, not requiring external programs (but usable only with [[BASH]] and KornShell): The fastest way, not requiring external programs (but not usable in Bourne shells):
Line 6: Line 6:
${#varname} # POSIX
"
${#varname}"
Line 8: 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 15:
expr "$varname" : '.*' # Bourne
expr "x$varname" : '.*' - 1
Line 15: 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 17: Line 21:
Another one: or:
Line 20: Line 24:
expr length "$varname" # Bourne, with GNU expr(1)
expr length "x$varname" - 1
Line 23: Line 28:
(This is for a BSD/GNU version of {{{expr}}}. Do not use this, because it is not [[POSIX]]). (BSD/GNU {{{expr}}} only)

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

A portable way is:
Line 26: Line 35:
${#arrayname[@]} expr \( "X$varname" : ".*" \) - 1
Line 29: Line 38:
Returns the Number of Elements in an Array. One may also use `awk`:
Line 32: Line 41:
${#arrayname[i]} # Bourne with POSIX awk
awk 'BEGIN {print length(ARGV[1])}' "$varname"
Line 35: Line 45:
Returns the length of the Arrays Element i. (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:

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

Expands to the number of elements in an array.

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

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

----
CategoryShell

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

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

# POSIX
"${#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:

# Bourne
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:

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

(BSD/GNU expr only)

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

A portable way is:

expr \( "X$varname" : ".*" \) - 1

One may also use awk:

# Bourne with POSIX awk
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:

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

Expands to the number of elements in an array.

# Korn/Bash
"${#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)