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







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.)










share|improve this question




















  • 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




















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.)










share|improve this question




















  • 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
















0












0








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.)










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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












3 Answers
3






active

oldest

votes


















12














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%





share|improve this answer


























  • 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 '%sn' "${TEST}"





share|improve this answer































    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"





    share|improve this answer
























      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%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









      12














      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%





      share|improve this answer


























      • 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
















      12














      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%





      share|improve this answer


























      • 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














      12












      12








      12







      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%





      share|improve this answer















      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%






      share|improve this answer














      share|improve this answer



      share|improve this answer








      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's printf, 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











      • "$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

















      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













      9














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



      printf '%sn' "${TEST}"





      share|improve this answer




























        9














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



        printf '%sn' "${TEST}"





        share|improve this answer


























          9












          9








          9







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



          printf '%sn' "${TEST}"





          share|improve this answer













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



          printf '%sn' "${TEST}"






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 16 at 15:32









          Stephen KittStephen Kitt

          190k26448524




          190k26448524























              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"





              share|improve this answer




























                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"





                share|improve this answer


























                  3












                  3








                  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"





                  share|improve this answer













                  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"






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered May 16 at 20:39









                  BMDanBMDan

                  1313




                  1313






























                      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%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





















































                      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

                      Bruad Bilen | Luke uk diar | NawigatsjuunCommonskategorii: BruadCommonskategorii: RunstükenWikiquote: Bruad

                      Modern approach to radio buttons

                      A word for worn pool table felt