Size: 830
Comment: what if $varname == "length" :-(
|
← Revision 16 as of 2015-03-05 00:24:26 ⇥
Size: 1522
Comment: syntax hl
|
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 5: | Line 5: |
{{{ ${#varname} |
{{{#!highlight bash # 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 11: | Line 14: |
{{{ expr "$varname" : '.*' |
{{{#!highlight bash # 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 19: | Line 23: |
{{{ expr length "$varname" |
{{{#!highlight bash # Bourne, with GNU expr(1) expr length "x$varname" - 1 |
Line 27: | Line 32: |
However, if {{{$varname}}} expands to {{{"length"}}}, the first version will fail with BSD/GNU {{{expr}}}. | One may also use `awk`: |
Line 29: | Line 34: |
{{{ ${#arrayname[@]} |
{{{#!highlight bash # Bourne with POSIX awk awk 'BEGIN {print length(ARGV[1])}' "$varname" |
Line 33: | Line 39: |
Returns the Number of Elements in an Array. | (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 35: | Line 42: |
{{{ ${#arrayname[i]} |
Similar needs: {{{#!highlight bash # Korn/Bash "${#arrayname[@]}" |
Line 39: | Line 49: |
Returns the length of the Arrays Element i. | Expands to the number of elements in an array. {{{#!highlight bash # Korn/Bash "${#arrayname[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):
(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:
(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:
(BSD/GNU expr only)
This second version is not specified in POSIX, so is not portable across all platforms.
One may also use awk:
(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:
Expands to the number of elements in an array.
Expands to the length of the array's element i.