What characters need to be escaped when using the printf command?












1















I want to clarify that I am not talking about how to escape characters on the shell level of interpretation.



As far as I can tell, only two character need to be escaped: % and



To print a literal %, you must escape it with a preceding %:



printf '%%'


To print a literal you must escape it with a preceding :



printf '\'


Are there any other instances where I would need to escape a character for it to be interpreted literally?










share|improve this question























  • looks like ' " ? .......... a good search engine for this kind of stuff is symbolhound.com

    – jsotola
    Jan 16 at 4:32













  • I don't want to be (too) rude, but this is really a RTFM question.

    – glenn jackman
    Jan 16 at 16:40
















1















I want to clarify that I am not talking about how to escape characters on the shell level of interpretation.



As far as I can tell, only two character need to be escaped: % and



To print a literal %, you must escape it with a preceding %:



printf '%%'


To print a literal you must escape it with a preceding :



printf '\'


Are there any other instances where I would need to escape a character for it to be interpreted literally?










share|improve this question























  • looks like ' " ? .......... a good search engine for this kind of stuff is symbolhound.com

    – jsotola
    Jan 16 at 4:32













  • I don't want to be (too) rude, but this is really a RTFM question.

    – glenn jackman
    Jan 16 at 16:40














1












1








1








I want to clarify that I am not talking about how to escape characters on the shell level of interpretation.



As far as I can tell, only two character need to be escaped: % and



To print a literal %, you must escape it with a preceding %:



printf '%%'


To print a literal you must escape it with a preceding :



printf '\'


Are there any other instances where I would need to escape a character for it to be interpreted literally?










share|improve this question














I want to clarify that I am not talking about how to escape characters on the shell level of interpretation.



As far as I can tell, only two character need to be escaped: % and



To print a literal %, you must escape it with a preceding %:



printf '%%'


To print a literal you must escape it with a preceding :



printf '\'


Are there any other instances where I would need to escape a character for it to be interpreted literally?







shell posix printf






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 16 at 4:19









Harold FischerHarold Fischer

663315




663315













  • looks like ' " ? .......... a good search engine for this kind of stuff is symbolhound.com

    – jsotola
    Jan 16 at 4:32













  • I don't want to be (too) rude, but this is really a RTFM question.

    – glenn jackman
    Jan 16 at 16:40



















  • looks like ' " ? .......... a good search engine for this kind of stuff is symbolhound.com

    – jsotola
    Jan 16 at 4:32













  • I don't want to be (too) rude, but this is really a RTFM question.

    – glenn jackman
    Jan 16 at 16:40

















looks like ' " ? .......... a good search engine for this kind of stuff is symbolhound.com

– jsotola
Jan 16 at 4:32







looks like ' " ? .......... a good search engine for this kind of stuff is symbolhound.com

– jsotola
Jan 16 at 4:32















I don't want to be (too) rude, but this is really a RTFM question.

– glenn jackman
Jan 16 at 16:40





I don't want to be (too) rude, but this is really a RTFM question.

– glenn jackman
Jan 16 at 16:40










1 Answer
1






active

oldest

votes


















0














From the manual:



$ man printf
...
printf FORMAT [ARGUMENT]...
...
FORMAT controls the output as in C printf. Interpreted sequences are:


This lists several interpreted sequences. The following are those where the character itself needs to be escaped.



   "     double quote
\ backslash
%% a single %


I tested these three in bash, and they behaved as expected. As per man bash, this implementation of printf uses the "standard printf(1) format specifications" as above, in addition to a few more that aren't relevant here.





However, other shells such as zsh implement printf slightly differently. Here, the double quote shouldn't be escaped.



$ printf '"'
"
$ printf '"'
"





share|improve this answer


























  • Yeah, same behavior here on dash and bash. For what it's worth, the dash manual makes no mention of needing the to escape ", but maybe I'm not reading in between the lines

    – Harold Fischer
    Jan 16 at 4:53













  • @HaroldFischer Presumably dash just inherits printf(1) too? I found the zsh manual a bit more opaque, so I didn't quote it here.

    – Sparhawk
    Jan 16 at 4:59











  • (edited) backslash-dquote is only needed if the format string is in dquotes, which is usually a bad idea, as then you also need to backslash backquote and (most) dollarsign, and may need to quadruple backslash if followed by a printf special. printf is builtin in bash and dash, but like all nonspecial builtins in a POSIX shell must also be present as an 'external' program.

    – dave_thompson_085
    Jan 16 at 5:09











  • @dave_thompson_085, the question does say I want to clarify that I am not talking about how to escape characters on the shell level of interpretation.

    – Sparhawk
    Jan 16 at 5:10











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f494725%2fwhat-characters-need-to-be-escaped-when-using-the-printf-command%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














From the manual:



$ man printf
...
printf FORMAT [ARGUMENT]...
...
FORMAT controls the output as in C printf. Interpreted sequences are:


This lists several interpreted sequences. The following are those where the character itself needs to be escaped.



   "     double quote
\ backslash
%% a single %


I tested these three in bash, and they behaved as expected. As per man bash, this implementation of printf uses the "standard printf(1) format specifications" as above, in addition to a few more that aren't relevant here.





However, other shells such as zsh implement printf slightly differently. Here, the double quote shouldn't be escaped.



$ printf '"'
"
$ printf '"'
"





share|improve this answer


























  • Yeah, same behavior here on dash and bash. For what it's worth, the dash manual makes no mention of needing the to escape ", but maybe I'm not reading in between the lines

    – Harold Fischer
    Jan 16 at 4:53













  • @HaroldFischer Presumably dash just inherits printf(1) too? I found the zsh manual a bit more opaque, so I didn't quote it here.

    – Sparhawk
    Jan 16 at 4:59











  • (edited) backslash-dquote is only needed if the format string is in dquotes, which is usually a bad idea, as then you also need to backslash backquote and (most) dollarsign, and may need to quadruple backslash if followed by a printf special. printf is builtin in bash and dash, but like all nonspecial builtins in a POSIX shell must also be present as an 'external' program.

    – dave_thompson_085
    Jan 16 at 5:09











  • @dave_thompson_085, the question does say I want to clarify that I am not talking about how to escape characters on the shell level of interpretation.

    – Sparhawk
    Jan 16 at 5:10
















0














From the manual:



$ man printf
...
printf FORMAT [ARGUMENT]...
...
FORMAT controls the output as in C printf. Interpreted sequences are:


This lists several interpreted sequences. The following are those where the character itself needs to be escaped.



   "     double quote
\ backslash
%% a single %


I tested these three in bash, and they behaved as expected. As per man bash, this implementation of printf uses the "standard printf(1) format specifications" as above, in addition to a few more that aren't relevant here.





However, other shells such as zsh implement printf slightly differently. Here, the double quote shouldn't be escaped.



$ printf '"'
"
$ printf '"'
"





share|improve this answer


























  • Yeah, same behavior here on dash and bash. For what it's worth, the dash manual makes no mention of needing the to escape ", but maybe I'm not reading in between the lines

    – Harold Fischer
    Jan 16 at 4:53













  • @HaroldFischer Presumably dash just inherits printf(1) too? I found the zsh manual a bit more opaque, so I didn't quote it here.

    – Sparhawk
    Jan 16 at 4:59











  • (edited) backslash-dquote is only needed if the format string is in dquotes, which is usually a bad idea, as then you also need to backslash backquote and (most) dollarsign, and may need to quadruple backslash if followed by a printf special. printf is builtin in bash and dash, but like all nonspecial builtins in a POSIX shell must also be present as an 'external' program.

    – dave_thompson_085
    Jan 16 at 5:09











  • @dave_thompson_085, the question does say I want to clarify that I am not talking about how to escape characters on the shell level of interpretation.

    – Sparhawk
    Jan 16 at 5:10














0












0








0







From the manual:



$ man printf
...
printf FORMAT [ARGUMENT]...
...
FORMAT controls the output as in C printf. Interpreted sequences are:


This lists several interpreted sequences. The following are those where the character itself needs to be escaped.



   "     double quote
\ backslash
%% a single %


I tested these three in bash, and they behaved as expected. As per man bash, this implementation of printf uses the "standard printf(1) format specifications" as above, in addition to a few more that aren't relevant here.





However, other shells such as zsh implement printf slightly differently. Here, the double quote shouldn't be escaped.



$ printf '"'
"
$ printf '"'
"





share|improve this answer















From the manual:



$ man printf
...
printf FORMAT [ARGUMENT]...
...
FORMAT controls the output as in C printf. Interpreted sequences are:


This lists several interpreted sequences. The following are those where the character itself needs to be escaped.



   "     double quote
\ backslash
%% a single %


I tested these three in bash, and they behaved as expected. As per man bash, this implementation of printf uses the "standard printf(1) format specifications" as above, in addition to a few more that aren't relevant here.





However, other shells such as zsh implement printf slightly differently. Here, the double quote shouldn't be escaped.



$ printf '"'
"
$ printf '"'
"






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 16 at 4:46

























answered Jan 16 at 4:38









SparhawkSparhawk

9,52263992




9,52263992













  • Yeah, same behavior here on dash and bash. For what it's worth, the dash manual makes no mention of needing the to escape ", but maybe I'm not reading in between the lines

    – Harold Fischer
    Jan 16 at 4:53













  • @HaroldFischer Presumably dash just inherits printf(1) too? I found the zsh manual a bit more opaque, so I didn't quote it here.

    – Sparhawk
    Jan 16 at 4:59











  • (edited) backslash-dquote is only needed if the format string is in dquotes, which is usually a bad idea, as then you also need to backslash backquote and (most) dollarsign, and may need to quadruple backslash if followed by a printf special. printf is builtin in bash and dash, but like all nonspecial builtins in a POSIX shell must also be present as an 'external' program.

    – dave_thompson_085
    Jan 16 at 5:09











  • @dave_thompson_085, the question does say I want to clarify that I am not talking about how to escape characters on the shell level of interpretation.

    – Sparhawk
    Jan 16 at 5:10



















  • Yeah, same behavior here on dash and bash. For what it's worth, the dash manual makes no mention of needing the to escape ", but maybe I'm not reading in between the lines

    – Harold Fischer
    Jan 16 at 4:53













  • @HaroldFischer Presumably dash just inherits printf(1) too? I found the zsh manual a bit more opaque, so I didn't quote it here.

    – Sparhawk
    Jan 16 at 4:59











  • (edited) backslash-dquote is only needed if the format string is in dquotes, which is usually a bad idea, as then you also need to backslash backquote and (most) dollarsign, and may need to quadruple backslash if followed by a printf special. printf is builtin in bash and dash, but like all nonspecial builtins in a POSIX shell must also be present as an 'external' program.

    – dave_thompson_085
    Jan 16 at 5:09











  • @dave_thompson_085, the question does say I want to clarify that I am not talking about how to escape characters on the shell level of interpretation.

    – Sparhawk
    Jan 16 at 5:10

















Yeah, same behavior here on dash and bash. For what it's worth, the dash manual makes no mention of needing the to escape ", but maybe I'm not reading in between the lines

– Harold Fischer
Jan 16 at 4:53







Yeah, same behavior here on dash and bash. For what it's worth, the dash manual makes no mention of needing the to escape ", but maybe I'm not reading in between the lines

– Harold Fischer
Jan 16 at 4:53















@HaroldFischer Presumably dash just inherits printf(1) too? I found the zsh manual a bit more opaque, so I didn't quote it here.

– Sparhawk
Jan 16 at 4:59





@HaroldFischer Presumably dash just inherits printf(1) too? I found the zsh manual a bit more opaque, so I didn't quote it here.

– Sparhawk
Jan 16 at 4:59













(edited) backslash-dquote is only needed if the format string is in dquotes, which is usually a bad idea, as then you also need to backslash backquote and (most) dollarsign, and may need to quadruple backslash if followed by a printf special. printf is builtin in bash and dash, but like all nonspecial builtins in a POSIX shell must also be present as an 'external' program.

– dave_thompson_085
Jan 16 at 5:09





(edited) backslash-dquote is only needed if the format string is in dquotes, which is usually a bad idea, as then you also need to backslash backquote and (most) dollarsign, and may need to quadruple backslash if followed by a printf special. printf is builtin in bash and dash, but like all nonspecial builtins in a POSIX shell must also be present as an 'external' program.

– dave_thompson_085
Jan 16 at 5:09













@dave_thompson_085, the question does say I want to clarify that I am not talking about how to escape characters on the shell level of interpretation.

– Sparhawk
Jan 16 at 5:10





@dave_thompson_085, the question does say I want to clarify that I am not talking about how to escape characters on the shell level of interpretation.

– Sparhawk
Jan 16 at 5:10


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f494725%2fwhat-characters-need-to-be-escaped-when-using-the-printf-command%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

How to make a Squid Proxy server?

Is this a new Fibonacci Identity?

19世紀