#459413 bash: Builtin command "printf" is not UTF-8 aware

Package:
bash
Source:
bash
Description:
GNU Bourne Again SHell
Submitter:
Teemu Likonen
Date:
2024-04-15 12:57:02 UTC
Severity:
wishlist
Tags:
#459413#5
Date:
2008-01-06 10:59:12 UTC
From:
To:
The bash builtin command "printf" is not UTF-8 aware (i.e. does not
handle UTF-8 multibyte characters). Example:

  $ /bin/bash -c 'type printf'
  printf is a shell builtin

  $ /bin/bash -c 'printf "!%5s!\n" a ä'
  !    a!
  !   ä!

"a" is "U+0061 LATIN SMALL LETTER A" (UTF-8: 0x61)
"ä" is "U+00E4 LATIN SMALL LETTER A WITH DIAERESIS" (UTF-8: 0xc3a4)

The problem is that the field width "%5s" is calculated wrong because
the letter "ä" takes two bytes in UTF-8 but is only one character. It
displays correctly because the terminal is UTF-8 aware.

(By the way, /usr/bin/printf [in coreutils] has the same bug.)

#459413#10
Date:
2008-01-06 12:47:07 UTC
From:
To:
Teemu Likonen <tlikonen@iki.fi> writes:

According to SUSv3, the printf utility interprets the format
string as specified under "File Format Notation", apart from
various exceptions which do not apply here.  And there, it says
that the field width and precision control the number of bytes,
rather than characters.  This is also how the printf function
works.

I suppose bash without --posix could be changed to behave
otherwise.  However, I doubt the usefulness of merely counting
Unicode characters.  If you want to align columns of a table, you
should also consider control characters, spacing and nonspacing
combining characters, and fullwidth ideographic characters.
If a simple character-counting feature were added now, perhaps
with new syntax such as "%5Uc", extending it later to handle
those cases too might cause compatibility problems.

#459413#15
Date:
2008-01-06 14:30:28 UTC
From:
To:
Kalle Olavi Niemitalo kirjoitti:

This sounds good idea. Yes, maybe the best would be to save the current
byte-counting method and add separate character-counting method.

This "%5s"-thing affects also mawk and gawk. I wish someone would update
the basic Unix/Linux tools to Unicode world. :) They are useful with
human languages too, not only with computer code.

#459413#22
Date:
2010-11-05 08:23:07 UTC
From:
To:
severity 459413 wishlist
tags 459413 + upstream
quit

Teemu Likonen wrote:

I agree.  Maybe someone should file it with http://austingroupbugs.net/?