Differences between revisions 1 and 12 (spanning 11 versions)
Revision 1 as of 2007-05-02 22:51:12
Size: 484
Editor: redondos
Comment:
Revision 12 as of 2013-07-24 17:20:09
Size: 1682
Editor: 188-223-3-27
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[Anchor(faq7)]] <<Anchor(faq7)>>
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 6:
# POSIX
Line 8: Line 10:
or (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:
Line 11: Line 15:
# Bourne
Line 14: 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`).)
Line 16: Line 21:
or or:
Line 19: Line 24:
# Bourne, with GNU expr(1)
Line 22: Line 28:
(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. However, if {{{$varname}}} expands to any `expr` operator like `+` or `(` or `length`, the first version will fail.

A portable way is:

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

One may also use `awk`:

{{{
# Bourne
awk -v x="$varname" 'BEGIN {print length(x)}'
}}}

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

Similar needs:

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

Returns the number of elements in an array.

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

Returns 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 "$varname" : '.*'

(expr prints the number of characters or bytes matching the pattern .*, which is the length of the string (in bytes for GNU expr).)

or:

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

(BSD/GNU expr only)

This second version is not specified in POSIX, so is not portable across all platforms. However, if $varname expands to any expr operator like + or ( or length, the first version will fail.

A portable way is:

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

One may also use awk:

# Bourne
awk -v x="$varname" 'BEGIN {print length(x)}'

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


Similar needs:

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

Returns the number of elements in an array.

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

Returns the length of the array's element i.


CategoryShell

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