Skip to content
Latchkey

bash printf format errors in CI

printf applies its format string to each argument and validates types. Passing a non-numeric value to %d, an incomplete conversion, or an argument that begins with - (read as an option) produces errors like "invalid number" or "not completed". Using a variable as the format string is also unsafe when it contains % or \.

What this error means

A step prints "printf: <value>: invalid number", "printf: %': missing format character", or mangled output when the data itself contains %` or backslashes.

bash
printf '%d\n' "3.5"        # printf: 3.5: invalid number
printf "$user_input\n"     # % or \ in the data is interpreted

Common causes

A non-integer passed to %d

printf's %d requires an integer. A float, empty string, or text triggers "invalid number" and prints 0 plus a warning.

Untrusted data used as the format string

Putting a variable in the format position means any % or \ in it is interpreted as a conversion or escape, producing errors or wrong output.

How to fix it

Use %s for arbitrary data

Keep a fixed literal format string and pass values as arguments to %s, so user data is never parsed as a format.

bash
printf '%s\n' "$user_input"

Validate numbers before %d

  1. Confirm the value is an integer before formatting it as one.
  2. Default empty values so %d does not see a blank.
  3. Use %f for floating-point values, not %d.
bash
n=${count:-0}
case "$n" in ''|*[!0-9]*) n=0 ;; esac
printf 'count: %d\n' "$n"

How to prevent it

  • Never pass untrusted data as the printf format string.
  • Use %s for text and validate integers before %d.
  • Prefer printf over echo for portable, predictable output.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →