0

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. – David Schwartz May 16 at 21:12
  • 1
    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 at 7:06
12

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%
  • Do you really need "${TEST}", can't it be "$TEST"? – Ferrybig May 16 at 17:15
  • "$TEST" is enough. – pLumo May 16 at 17:41
  • 2
    +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 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). – duskwuff May 16 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 at 1:56
9

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

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

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"
New contributor
BMDan is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.