Skip to content
James Morris edited this page Sep 27, 2023 · 8 revisions

Don't use variables in the printf format string. Use printf "..%s.." "$foo".

Problematic code:

printf "Hello, $NAME\n"

Correct code:

printf "Hello, %s\n" "$NAME"

Rationale:

printf interprets escape sequences and format specifiers in the format string. If variables are included, any escape sequences or format specifiers in the data will be interpreted too, when you most likely wanted to treat it as data. Example:

coverage='96%'
printf "Unit test coverage: %s\n" "$coverage"
printf "Unit test coverage: $coverage\n"

The first printf writes Unit test coverage: 96%.

The second writes bash: printf: `\': invalid format character

Sometimes you may actually want to interpret data as a format string, like in:

octToAscii() { printf "\\$1"; }
octToAscii 130

In this case, use the %b format specifier that expands escape sequences without interpreting other format specifiers:

octToAscii() { printf '%b' "\0$1"; }
octToAscii 130

Exceptions:

Sometimes you might have a pattern in a variable:

filepattern="file-%d.jpg"
printf -v filename "$filepattern" "$number"

This has no good rewrite. Please ignore the warning with a directive.

Related resources:

ShellCheck

Each individual ShellCheck warning has its own wiki page like SC1000. Use GitHub Wiki's "Pages" feature guerraart8 to find a specific , or see Checks.

Clone this wiki locally