Using `printf` to print variable containing `%` percent sign results in "bash: printf: `p': invalid format character"

Use printf in its normal form:

printf '%s\n' "${TEST}"

From man printf:

SYNOPSIS
printf FORMAT [ARGUMENT]...

You should never pass a variable to the FORMAT string as it may lead to errors and security vulnerabilities.


Btw:

if you want to have % sign as part of the FORMAT, you need to enter %%, e.g.:

$ printf '%d%%\n' 100
100%

You should never put variable content in the format string given to printf. Use this instead:

printf '%s\n' "${TEST}"

printf takes one guaranteed parameter, and then a number of additional parameters based on what you pass. So something like this:

printf '%07.2f' 5

gets turned into:

0005.00

The first parameter, called "format", is always present. If it contains no %strings, it's simply printed. Thus:

printf Hello

produces simply Hello (notably, without the trailing newline echo would add in its default mode). In expecting your example to work, you have been misled by your own previous (unknowing) abuse of this fact; because you only passed strings without %s into format, from printf's point of view, you kept asking it to output things that required no substitutions, so it pretty much functioned like echo -ne.

If you want to do this right, you probably want to start forming your printable strings with printf's builtin substitution capabilities. Lines like this appear all over my code:

printf '%20s: %6d %05d.%02d%%' "$key" $val $((val/max)) $((val*100/max%100))

If you want exactly what you're currently doing now to work, you want echo -ne, as so:

TEST="contains % percent"
echo -ne "${TEST}\n"

That preserves the (questionable) behavior of interpreting \ escapes inside the variable. It also seems a little silly to supply -n and then stick the \n back on, but I offer it because it's a global find-and-replace you can apply to everything you're current doing. A cleaner version that still keeps \ escapes working in the variable would be:

TEST="contains % percent"
echo -e "$TEST"