I want to use printf to print a variable. It might be possible that this variable contains a % percent sign.

Minimal example:

$ TEST="contains % percent"
$ echo "${TEST}"
contains % percent
$ printf "${TEST}\n"
bash: printf: `p': invalid format character
contains $

(echo provides the desired output.)

  • 1
    The docs are pretty clear that the first parameter is supposed to be a format string. May 16, 2019 at 21:12
  • 8
    Not sure why some people need to downvote this. A lot of newcomers will make that mistake, so imo it's a very valuable question.
    – pLumo
    May 17, 2019 at 7:06
  • I have %variables% in my browser like %OS% and %VERSION%. How to print those? Dec 13, 2021 at 19:51
  • You want print, not printf. I made the same mistake.
    – MiloNC
    Jan 25, 2022 at 22:19

3 Answers 3


Use printf in its normal form:

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

From man printf:


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


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

$ printf '%d%%\n' 100
  • 2
    Do you really need "${TEST}", can't it be "$TEST"?
    – Ferrybig
    May 16, 2019 at 17:15
  • 1
    "$TEST" is enough.
    – pLumo
    May 16, 2019 at 17:41
  • 3
    +1. But it's not just 'normal', it's necessary. Passing a variable to printf's first parameter is Christmas to hackers. Never do it. That is part of secure software dev 101.
    – Jeffrey
    May 16, 2019 at 21:24
  • @Jeffrey Most of the potential vulnerabilities in printf are only applicable to the C function. There aren't as many evil things you can do by passing a bad format string to /usr/bin/printf (or the equivalent shell builtin).
    – user130738
    May 16, 2019 at 21:56
  • @duskwuff you can do variable assignment using bash's printf, and variable assignments can lead to a lot more (e.g., shellshock in days past)
    – muru
    May 17, 2019 at 1:56

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:


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"
  • This should be the accepted answer. As it actually has a working fix. The accepted answer talks about passing variables. But it is clear that there is no intention to pass variables, just the string has a % sign. which in my case I have no control over.
    – Stripy42
    Mar 25 at 8:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .