How to increment the value of a (decimal) variable (with leading zero) by +1?Addition with 'sed'Increment number in bash variable stringIncrement Daily Config ValueIncrement a variable using a Bash scriptincrement a value pulled from a fileUsing awk to divide the number in each line of a file by the maximum value in that fileHow to propagate the value of a variableread a file from Server path - bashString to integer in Shell

Christmas party at employers home

Why do these two ways of understanding constant acceleration give different results?

How to treat unhandled exceptions? (Terminate the application vs. Keep it alive)

How to deal with people whose priority is to not get blamed?

Mapping string into integers

Where does the upgrade to macOS Catalina move root "/" directory files?

Demod as a neologism

Why is Trump releasing or not of his taxes such a big deal?

Are there 99 percentiles, or 100 percentiles? And are they groups of numbers, or dividers or pointers to individual numbers?

What is the gold linker?

Can Chandra, Acolyte of Flame cast Heartfire without the additional cost?

Why is my paper "under review" if it contains no results?

How can I learn to write better questions to test for conceptual understanding?

What's the meaning of java.util.@Nullable?

Why does Principal Vagina say, "no relation" after introducing himself?

Encountering former, abusive advisor at a conference

Why are second inversion triads considered less consonant than first inversion triads?

Is the tap water in France safe to drink?

Uniform Roe algebra of virtually abelian group is type I C*-algebra?

How to balance combat for a duet campaign with non-frontliner classes?

Can a Pokemon that I tried to capture from field research run away?

Can we not simply connect a battery to a RAM to prevent data loss during power cuts?

Does any politician honestly want a No Deal Brexit?

On notice period - coworker I need to train is giving me the silent treatment



How to increment the value of a (decimal) variable (with leading zero) by +1?


Addition with 'sed'Increment number in bash variable stringIncrement Daily Config ValueIncrement a variable using a Bash scriptincrement a value pulled from a fileUsing awk to divide the number in each line of a file by the maximum value in that fileHow to propagate the value of a variableread a file from Server path - bashString to integer in Shell






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









2

















I have a file in the name of Build.number with the content value 012 which I need to increment by +1.
So, I tried this



BN=$($cat Build.number)
BN=$(($BN+1))
echo $BN >Build.number


but here I am getting the value 11 when I am expecting 013.
Can anyone help me?










share|improve this question









New contributor



user11668570 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























  • There are two good answers, both not only explaining the cause of the problem, but providing additional information that might be useful. Stephen's provides a reference and Stéphane's mentions other shells and provides a more efficient way of reading the data without using the cat program. The ideal answer would combine both. (The ideal answer would also use "%.3dn" format rather than the old leading 0 trick, which is ancient style, and misleading because in that case the leading 0 doesn't mean octal.)

    – Ray Butterworth
    8 hours ago












  • Related: unix.stackexchange.com/a/36959/117549

    – Jeff Schaller
    8 hours ago






  • 1





    @Ray why is .3d better than 03d? Apart from the octal confusion...

    – Stephen Kitt
    8 hours ago






  • 1





    @StephenKitt. For d, i, o, u, x, X, a, A, e, E, f, F, g, and G conversion specifiers, leading zeros (following any indication of sign or base) are used to pad to the field width rather than performing space padding, except when converting an infinity or NaN. If the 0 and - flags both appear, the 0 flag is ignored. For d, i, o, u, x, and X conversion specifiers, if a precision is specified, the 0 flag shall be ignored. [CX] [Option Start] If the 0 and <apostrophe> flags both appear, the grouping characters are inserted before zero padding. For other conversions, the behavior is undefined.

    – Ray Butterworth
    5 hours ago






  • 2





    @StephenKitt, the .3d makes the intent much more obvious: An optional precision that gives the minimum number of digits to appear for the d, i, o, u, x, and X conversion specifiers. The 03d syntax dates back to the original 1970s version, and preserving backward compatibility has made its rules extremely complicated.

    – Ray Butterworth
    5 hours ago


















2

















I have a file in the name of Build.number with the content value 012 which I need to increment by +1.
So, I tried this



BN=$($cat Build.number)
BN=$(($BN+1))
echo $BN >Build.number


but here I am getting the value 11 when I am expecting 013.
Can anyone help me?










share|improve this question









New contributor



user11668570 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























  • There are two good answers, both not only explaining the cause of the problem, but providing additional information that might be useful. Stephen's provides a reference and Stéphane's mentions other shells and provides a more efficient way of reading the data without using the cat program. The ideal answer would combine both. (The ideal answer would also use "%.3dn" format rather than the old leading 0 trick, which is ancient style, and misleading because in that case the leading 0 doesn't mean octal.)

    – Ray Butterworth
    8 hours ago












  • Related: unix.stackexchange.com/a/36959/117549

    – Jeff Schaller
    8 hours ago






  • 1





    @Ray why is .3d better than 03d? Apart from the octal confusion...

    – Stephen Kitt
    8 hours ago






  • 1





    @StephenKitt. For d, i, o, u, x, X, a, A, e, E, f, F, g, and G conversion specifiers, leading zeros (following any indication of sign or base) are used to pad to the field width rather than performing space padding, except when converting an infinity or NaN. If the 0 and - flags both appear, the 0 flag is ignored. For d, i, o, u, x, and X conversion specifiers, if a precision is specified, the 0 flag shall be ignored. [CX] [Option Start] If the 0 and <apostrophe> flags both appear, the grouping characters are inserted before zero padding. For other conversions, the behavior is undefined.

    – Ray Butterworth
    5 hours ago






  • 2





    @StephenKitt, the .3d makes the intent much more obvious: An optional precision that gives the minimum number of digits to appear for the d, i, o, u, x, and X conversion specifiers. The 03d syntax dates back to the original 1970s version, and preserving backward compatibility has made its rules extremely complicated.

    – Ray Butterworth
    5 hours ago














2












2








2








I have a file in the name of Build.number with the content value 012 which I need to increment by +1.
So, I tried this



BN=$($cat Build.number)
BN=$(($BN+1))
echo $BN >Build.number


but here I am getting the value 11 when I am expecting 013.
Can anyone help me?










share|improve this question









New contributor



user11668570 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I have a file in the name of Build.number with the content value 012 which I need to increment by +1.
So, I tried this



BN=$($cat Build.number)
BN=$(($BN+1))
echo $BN >Build.number


but here I am getting the value 11 when I am expecting 013.
Can anyone help me?







bash shell-script numeric-data arithmetic






share|improve this question









New contributor



user11668570 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor



user11668570 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question








edited 4 hours ago









Cbhihe

4821 gold badge6 silver badges20 bronze badges




4821 gold badge6 silver badges20 bronze badges






New contributor



user11668570 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked 9 hours ago









user11668570user11668570

191 bronze badge




191 bronze badge




New contributor



user11668570 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




user11668570 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

















  • There are two good answers, both not only explaining the cause of the problem, but providing additional information that might be useful. Stephen's provides a reference and Stéphane's mentions other shells and provides a more efficient way of reading the data without using the cat program. The ideal answer would combine both. (The ideal answer would also use "%.3dn" format rather than the old leading 0 trick, which is ancient style, and misleading because in that case the leading 0 doesn't mean octal.)

    – Ray Butterworth
    8 hours ago












  • Related: unix.stackexchange.com/a/36959/117549

    – Jeff Schaller
    8 hours ago






  • 1





    @Ray why is .3d better than 03d? Apart from the octal confusion...

    – Stephen Kitt
    8 hours ago






  • 1





    @StephenKitt. For d, i, o, u, x, X, a, A, e, E, f, F, g, and G conversion specifiers, leading zeros (following any indication of sign or base) are used to pad to the field width rather than performing space padding, except when converting an infinity or NaN. If the 0 and - flags both appear, the 0 flag is ignored. For d, i, o, u, x, and X conversion specifiers, if a precision is specified, the 0 flag shall be ignored. [CX] [Option Start] If the 0 and <apostrophe> flags both appear, the grouping characters are inserted before zero padding. For other conversions, the behavior is undefined.

    – Ray Butterworth
    5 hours ago






  • 2





    @StephenKitt, the .3d makes the intent much more obvious: An optional precision that gives the minimum number of digits to appear for the d, i, o, u, x, and X conversion specifiers. The 03d syntax dates back to the original 1970s version, and preserving backward compatibility has made its rules extremely complicated.

    – Ray Butterworth
    5 hours ago


















  • There are two good answers, both not only explaining the cause of the problem, but providing additional information that might be useful. Stephen's provides a reference and Stéphane's mentions other shells and provides a more efficient way of reading the data without using the cat program. The ideal answer would combine both. (The ideal answer would also use "%.3dn" format rather than the old leading 0 trick, which is ancient style, and misleading because in that case the leading 0 doesn't mean octal.)

    – Ray Butterworth
    8 hours ago












  • Related: unix.stackexchange.com/a/36959/117549

    – Jeff Schaller
    8 hours ago






  • 1





    @Ray why is .3d better than 03d? Apart from the octal confusion...

    – Stephen Kitt
    8 hours ago






  • 1





    @StephenKitt. For d, i, o, u, x, X, a, A, e, E, f, F, g, and G conversion specifiers, leading zeros (following any indication of sign or base) are used to pad to the field width rather than performing space padding, except when converting an infinity or NaN. If the 0 and - flags both appear, the 0 flag is ignored. For d, i, o, u, x, and X conversion specifiers, if a precision is specified, the 0 flag shall be ignored. [CX] [Option Start] If the 0 and <apostrophe> flags both appear, the grouping characters are inserted before zero padding. For other conversions, the behavior is undefined.

    – Ray Butterworth
    5 hours ago






  • 2





    @StephenKitt, the .3d makes the intent much more obvious: An optional precision that gives the minimum number of digits to appear for the d, i, o, u, x, and X conversion specifiers. The 03d syntax dates back to the original 1970s version, and preserving backward compatibility has made its rules extremely complicated.

    – Ray Butterworth
    5 hours ago

















There are two good answers, both not only explaining the cause of the problem, but providing additional information that might be useful. Stephen's provides a reference and Stéphane's mentions other shells and provides a more efficient way of reading the data without using the cat program. The ideal answer would combine both. (The ideal answer would also use "%.3dn" format rather than the old leading 0 trick, which is ancient style, and misleading because in that case the leading 0 doesn't mean octal.)

– Ray Butterworth
8 hours ago






There are two good answers, both not only explaining the cause of the problem, but providing additional information that might be useful. Stephen's provides a reference and Stéphane's mentions other shells and provides a more efficient way of reading the data without using the cat program. The ideal answer would combine both. (The ideal answer would also use "%.3dn" format rather than the old leading 0 trick, which is ancient style, and misleading because in that case the leading 0 doesn't mean octal.)

– Ray Butterworth
8 hours ago














Related: unix.stackexchange.com/a/36959/117549

– Jeff Schaller
8 hours ago





Related: unix.stackexchange.com/a/36959/117549

– Jeff Schaller
8 hours ago




1




1





@Ray why is .3d better than 03d? Apart from the octal confusion...

– Stephen Kitt
8 hours ago





@Ray why is .3d better than 03d? Apart from the octal confusion...

– Stephen Kitt
8 hours ago




1




1





@StephenKitt. For d, i, o, u, x, X, a, A, e, E, f, F, g, and G conversion specifiers, leading zeros (following any indication of sign or base) are used to pad to the field width rather than performing space padding, except when converting an infinity or NaN. If the 0 and - flags both appear, the 0 flag is ignored. For d, i, o, u, x, and X conversion specifiers, if a precision is specified, the 0 flag shall be ignored. [CX] [Option Start] If the 0 and <apostrophe> flags both appear, the grouping characters are inserted before zero padding. For other conversions, the behavior is undefined.

– Ray Butterworth
5 hours ago





@StephenKitt. For d, i, o, u, x, X, a, A, e, E, f, F, g, and G conversion specifiers, leading zeros (following any indication of sign or base) are used to pad to the field width rather than performing space padding, except when converting an infinity or NaN. If the 0 and - flags both appear, the 0 flag is ignored. For d, i, o, u, x, and X conversion specifiers, if a precision is specified, the 0 flag shall be ignored. [CX] [Option Start] If the 0 and <apostrophe> flags both appear, the grouping characters are inserted before zero padding. For other conversions, the behavior is undefined.

– Ray Butterworth
5 hours ago




2




2





@StephenKitt, the .3d makes the intent much more obvious: An optional precision that gives the minimum number of digits to appear for the d, i, o, u, x, and X conversion specifiers. The 03d syntax dates back to the original 1970s version, and preserving backward compatibility has made its rules extremely complicated.

– Ray Butterworth
5 hours ago






@StephenKitt, the .3d makes the intent much more obvious: An optional precision that gives the minimum number of digits to appear for the d, i, o, u, x, and X conversion specifiers. The 03d syntax dates back to the original 1970s version, and preserving backward compatibility has made its rules extremely complicated.

– Ray Butterworth
5 hours ago











2 Answers
2






active

oldest

votes


















11


















The leading 0 causes Bash to interpret the value as an octal value; 012 octal is 10 decimal, so you get 11.



To force the use of decimal, add 10#:



BN=10#$(cat Build.number)
echo $((++BN)) > Build.number


To print the number using at least three digits, use printf:



printf "%.3dn" $((++BN)) > Build.number





share|improve this answer



































    6


















    bash treats constants that start with 0 as octal numbers in its arithmetic expressions, so 011 is actually 9.



    That's actually a POSIX requirement.



    Some other shells like mksh or zsh ignore it (unless in POSIX compliant mode) as it gets in the way far more often than it is useful.



    With ksh93, BN=011; echo "$(($BN))" outputs 9, but echo "$((BN))" outputs 11.



    In bash, you can use BN=$((10#$(<Build.number))), which should work as long as the number doesn't start with - or +.






    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/4.0/"u003ecc by-sa 4.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
      );



      );







      user11668570 is a new contributor. Be nice, and check out our Code of Conduct.









      draft saved

      draft discarded
















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f545909%2fhow-to-increment-the-value-of-a-decimal-variable-with-leading-zero-by-1%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown


























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      11


















      The leading 0 causes Bash to interpret the value as an octal value; 012 octal is 10 decimal, so you get 11.



      To force the use of decimal, add 10#:



      BN=10#$(cat Build.number)
      echo $((++BN)) > Build.number


      To print the number using at least three digits, use printf:



      printf "%.3dn" $((++BN)) > Build.number





      share|improve this answer
































        11


















        The leading 0 causes Bash to interpret the value as an octal value; 012 octal is 10 decimal, so you get 11.



        To force the use of decimal, add 10#:



        BN=10#$(cat Build.number)
        echo $((++BN)) > Build.number


        To print the number using at least three digits, use printf:



        printf "%.3dn" $((++BN)) > Build.number





        share|improve this answer






























          11














          11










          11









          The leading 0 causes Bash to interpret the value as an octal value; 012 octal is 10 decimal, so you get 11.



          To force the use of decimal, add 10#:



          BN=10#$(cat Build.number)
          echo $((++BN)) > Build.number


          To print the number using at least three digits, use printf:



          printf "%.3dn" $((++BN)) > Build.number





          share|improve this answer
















          The leading 0 causes Bash to interpret the value as an octal value; 012 octal is 10 decimal, so you get 11.



          To force the use of decimal, add 10#:



          BN=10#$(cat Build.number)
          echo $((++BN)) > Build.number


          To print the number using at least three digits, use printf:



          printf "%.3dn" $((++BN)) > Build.number






          share|improve this answer















          share|improve this answer




          share|improve this answer








          edited 53 mins ago

























          answered 9 hours ago









          Stephen KittStephen Kitt

          208k27 gold badges493 silver badges559 bronze badges




          208k27 gold badges493 silver badges559 bronze badges


























              6


















              bash treats constants that start with 0 as octal numbers in its arithmetic expressions, so 011 is actually 9.



              That's actually a POSIX requirement.



              Some other shells like mksh or zsh ignore it (unless in POSIX compliant mode) as it gets in the way far more often than it is useful.



              With ksh93, BN=011; echo "$(($BN))" outputs 9, but echo "$((BN))" outputs 11.



              In bash, you can use BN=$((10#$(<Build.number))), which should work as long as the number doesn't start with - or +.






              share|improve this answer






























                6


















                bash treats constants that start with 0 as octal numbers in its arithmetic expressions, so 011 is actually 9.



                That's actually a POSIX requirement.



                Some other shells like mksh or zsh ignore it (unless in POSIX compliant mode) as it gets in the way far more often than it is useful.



                With ksh93, BN=011; echo "$(($BN))" outputs 9, but echo "$((BN))" outputs 11.



                In bash, you can use BN=$((10#$(<Build.number))), which should work as long as the number doesn't start with - or +.






                share|improve this answer




























                  6














                  6










                  6









                  bash treats constants that start with 0 as octal numbers in its arithmetic expressions, so 011 is actually 9.



                  That's actually a POSIX requirement.



                  Some other shells like mksh or zsh ignore it (unless in POSIX compliant mode) as it gets in the way far more often than it is useful.



                  With ksh93, BN=011; echo "$(($BN))" outputs 9, but echo "$((BN))" outputs 11.



                  In bash, you can use BN=$((10#$(<Build.number))), which should work as long as the number doesn't start with - or +.






                  share|improve this answer














                  bash treats constants that start with 0 as octal numbers in its arithmetic expressions, so 011 is actually 9.



                  That's actually a POSIX requirement.



                  Some other shells like mksh or zsh ignore it (unless in POSIX compliant mode) as it gets in the way far more often than it is useful.



                  With ksh93, BN=011; echo "$(($BN))" outputs 9, but echo "$((BN))" outputs 11.



                  In bash, you can use BN=$((10#$(<Build.number))), which should work as long as the number doesn't start with - or +.







                  share|improve this answer













                  share|improve this answer




                  share|improve this answer










                  answered 9 hours ago









                  Stéphane ChazelasStéphane Chazelas

                  337k58 gold badges661 silver badges1038 bronze badges




                  337k58 gold badges661 silver badges1038 bronze badges
























                      user11668570 is a new contributor. Be nice, and check out our Code of Conduct.









                      draft saved

                      draft discarded

















                      user11668570 is a new contributor. Be nice, and check out our Code of Conduct.












                      user11668570 is a new contributor. Be nice, and check out our Code of Conduct.











                      user11668570 is a new contributor. Be nice, and check out our Code of Conduct.














                      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%2f545909%2fhow-to-increment-the-value-of-a-decimal-variable-with-leading-zero-by-1%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

                      Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                      Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                      François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480