Using `printf` to print variable containing `%` percent sign results in “bash: printf: `p': invalid format...
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom: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.)
bash variable echo escape-characters printf
add a comment |
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.)
bash variable echo escape-characters printf
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
add a comment |
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.)
bash variable echo escape-characters printf
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.)
bash variable echo escape-characters printf
bash variable echo escape-characters printf
edited May 16 at 15:32
finefoot
asked May 16 at 15:31
finefootfinefoot
424112
424112
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
add a comment |
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
1
1
The docs are pretty clear that the first parameter is supposed to be a format string.
– David Schwartz
May 16 at 21:12
The docs are pretty clear that the first parameter is supposed to be a format string.
– David Schwartz
May 16 at 21:12
1
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
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
add a comment |
3 Answers
3
active
oldest
votes
Use printf in its normal form:
printf '%sn' "${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'sprintf, and variable assignments can lead to a lot more (e.g., shellshock in days past)
– muru
May 17 at 1:56
|
show 1 more comment
You should never put variable content in the format string given to printf. Use this instead:
printf '%sn' "${TEST}"
add a comment |
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"
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f519315%2fusing-printf-to-print-variable-containing-percent-sign-results-in-bash-p%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use printf in its normal form:
printf '%sn' "${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'sprintf, and variable assignments can lead to a lot more (e.g., shellshock in days past)
– muru
May 17 at 1:56
|
show 1 more comment
Use printf in its normal form:
printf '%sn' "${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'sprintf, and variable assignments can lead to a lot more (e.g., shellshock in days past)
– muru
May 17 at 1:56
|
show 1 more comment
Use printf in its normal form:
printf '%sn' "${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%
Use printf in its normal form:
printf '%sn' "${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%
edited May 17 at 7:01
answered May 16 at 15:32
pLumopLumo
5,9371225
5,9371225
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'sprintf, and variable assignments can lead to a lot more (e.g., shellshock in days past)
– muru
May 17 at 1:56
|
show 1 more comment
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'sprintf, and variable assignments can lead to a lot more (e.g., shellshock in days past)
– muru
May 17 at 1:56
Do you really need
"${TEST}", can't it be "$TEST"?– Ferrybig
May 16 at 17:15
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
"$TEST" is enough.– pLumo
May 16 at 17:41
2
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
+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
@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
@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
|
show 1 more comment
You should never put variable content in the format string given to printf. Use this instead:
printf '%sn' "${TEST}"
add a comment |
You should never put variable content in the format string given to printf. Use this instead:
printf '%sn' "${TEST}"
add a comment |
You should never put variable content in the format string given to printf. Use this instead:
printf '%sn' "${TEST}"
You should never put variable content in the format string given to printf. Use this instead:
printf '%sn' "${TEST}"
answered May 16 at 15:32
Stephen KittStephen Kitt
190k26448524
190k26448524
add a comment |
add a comment |
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"
add a comment |
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"
add a comment |
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"
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"
answered May 16 at 20:39
BMDanBMDan
1313
1313
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f519315%2fusing-printf-to-print-variable-containing-percent-sign-results-in-bash-p%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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