Multibyte-capable printf() for bash scripts
Find a file
2026-08-29 20:48:00 +02:00
LICENSE Initial commit 2025-07-13 16:42:50 +02:00
mbprintf v1.0: ANSI escape sequences 2026-08-29 20:48:00 +02:00
README.md Initial code. 2025-07-13 16:58:27 +02:00
test_mbprintf.sh v1.0: ANSI escape sequences 2026-08-29 20:48:00 +02:00

mbprintf

Multibyte-capable printf for bash scripts

Purpose

The bash builtin printf function has a significant bug:

When strings contain multibyte (non-ASCII-7) characters, the formatting is off -- because bash printf counts bytes, not printable characters.

Since this is all kinds of annoying, I wrote a shell function mbprintf that does fix this error.

Implementation

The mbprintf function iterates through the format string, eating either a verbatim sequence or a conversion specifier. Conversion specifiers that are not 's' specifiers are forwarded to printf, while 's' specifiers are handled by a helper function that does the formatting manually, using ${#string} to determine the string length in printable characters instead of bytes.

Status

I didn't bother to add extra handling for 'c' conversions, as I figured they would not be commonly used in bash scripts.

Other than that, the function should work, and so I consider this code to be beta quality.