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.
printf '%d\n' "3.5" # printf: 3.5: invalid number
printf "$user_input\n" # % or \ in the data is interpretedCommon 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.
printf '%s\n' "$user_input"Validate numbers before %d
- Confirm the value is an integer before formatting it as one.
- Default empty values so
%ddoes not see a blank. - Use
%ffor floating-point values, not%d.
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
%sfor text and validate integers before%d. - Prefer
printfoverechofor portable, predictable output.