How to align a string variable in Bash

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I'm trying to align a string variable in Bash. This is the desired behaviour.



if str1="123" , then str2=" 123"
if str1="1234" , then str2=" 1234"
if str1="12345", then str2=" 12345"


etc.
I've seen how to do it to print with printf, but I need to do it inside the variable without printing it out.










share|improve this question









New contributor




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























    up vote
    1
    down vote

    favorite












    I'm trying to align a string variable in Bash. This is the desired behaviour.



    if str1="123" , then str2=" 123"
    if str1="1234" , then str2=" 1234"
    if str1="12345", then str2=" 12345"


    etc.
    I've seen how to do it to print with printf, but I need to do it inside the variable without printing it out.










    share|improve this question









    New contributor




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





















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'm trying to align a string variable in Bash. This is the desired behaviour.



      if str1="123" , then str2=" 123"
      if str1="1234" , then str2=" 1234"
      if str1="12345", then str2=" 12345"


      etc.
      I've seen how to do it to print with printf, but I need to do it inside the variable without printing it out.










      share|improve this question









      New contributor




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











      I'm trying to align a string variable in Bash. This is the desired behaviour.



      if str1="123" , then str2=" 123"
      if str1="1234" , then str2=" 1234"
      if str1="12345", then str2=" 12345"


      etc.
      I've seen how to do it to print with printf, but I need to do it inside the variable without printing it out.







      bash text-formatting






      share|improve this question









      New contributor




      Jairo Alves 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




      Jairo Alves 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 6 mins ago









      Jeff Schaller

      35.1k952115




      35.1k952115






      New contributor




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









      asked 1 hour ago









      Jairo Alves

      61




      61




      New contributor




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





      New contributor





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






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




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote













          Assuming the variable only contains single-byte, single-width characters:



          printf -v str2 %8s "$str1"


          If they may contain multi-byte (but still single-width) characters, you can do instead:



          printf -v str2 %8s%s '' "$str1"
          str2=$str2: -8


          (note however that that one truncates values larger than 8 characters).



          Here bash's printf builtin command supports -v to store the result of printf into a variable, but even if it didn't you could use command substitution:



          str2=$(printf %8s "$str1")





          share|improve this answer






















          • Thank you, very much! I'm trying it now and it will most probably do what I needed.It will help a lot! :)
            – Jairo Alves
            1 hour ago










          • If I'm understanding it correctly, the -v makes the printf command abstain from printing the result on the screen, instead attributting it to the variable that follows it, is that correct?
            – Jairo Alves
            1 hour ago











          • @JairoAlves, yes. See info bash printf or help printf within bash for details.
            – Stéphane Chazelas
            1 hour ago










          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',
          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
          );



          );






          Jairo Alves 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%2f480170%2fhow-to-align-a-string-variable-in-bash%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote













          Assuming the variable only contains single-byte, single-width characters:



          printf -v str2 %8s "$str1"


          If they may contain multi-byte (but still single-width) characters, you can do instead:



          printf -v str2 %8s%s '' "$str1"
          str2=$str2: -8


          (note however that that one truncates values larger than 8 characters).



          Here bash's printf builtin command supports -v to store the result of printf into a variable, but even if it didn't you could use command substitution:



          str2=$(printf %8s "$str1")





          share|improve this answer






















          • Thank you, very much! I'm trying it now and it will most probably do what I needed.It will help a lot! :)
            – Jairo Alves
            1 hour ago










          • If I'm understanding it correctly, the -v makes the printf command abstain from printing the result on the screen, instead attributting it to the variable that follows it, is that correct?
            – Jairo Alves
            1 hour ago











          • @JairoAlves, yes. See info bash printf or help printf within bash for details.
            – Stéphane Chazelas
            1 hour ago














          up vote
          3
          down vote













          Assuming the variable only contains single-byte, single-width characters:



          printf -v str2 %8s "$str1"


          If they may contain multi-byte (but still single-width) characters, you can do instead:



          printf -v str2 %8s%s '' "$str1"
          str2=$str2: -8


          (note however that that one truncates values larger than 8 characters).



          Here bash's printf builtin command supports -v to store the result of printf into a variable, but even if it didn't you could use command substitution:



          str2=$(printf %8s "$str1")





          share|improve this answer






















          • Thank you, very much! I'm trying it now and it will most probably do what I needed.It will help a lot! :)
            – Jairo Alves
            1 hour ago










          • If I'm understanding it correctly, the -v makes the printf command abstain from printing the result on the screen, instead attributting it to the variable that follows it, is that correct?
            – Jairo Alves
            1 hour ago











          • @JairoAlves, yes. See info bash printf or help printf within bash for details.
            – Stéphane Chazelas
            1 hour ago












          up vote
          3
          down vote










          up vote
          3
          down vote









          Assuming the variable only contains single-byte, single-width characters:



          printf -v str2 %8s "$str1"


          If they may contain multi-byte (but still single-width) characters, you can do instead:



          printf -v str2 %8s%s '' "$str1"
          str2=$str2: -8


          (note however that that one truncates values larger than 8 characters).



          Here bash's printf builtin command supports -v to store the result of printf into a variable, but even if it didn't you could use command substitution:



          str2=$(printf %8s "$str1")





          share|improve this answer














          Assuming the variable only contains single-byte, single-width characters:



          printf -v str2 %8s "$str1"


          If they may contain multi-byte (but still single-width) characters, you can do instead:



          printf -v str2 %8s%s '' "$str1"
          str2=$str2: -8


          (note however that that one truncates values larger than 8 characters).



          Here bash's printf builtin command supports -v to store the result of printf into a variable, but even if it didn't you could use command substitution:



          str2=$(printf %8s "$str1")






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 1 hour ago

























          answered 1 hour ago









          Stéphane Chazelas

          292k54543882




          292k54543882











          • Thank you, very much! I'm trying it now and it will most probably do what I needed.It will help a lot! :)
            – Jairo Alves
            1 hour ago










          • If I'm understanding it correctly, the -v makes the printf command abstain from printing the result on the screen, instead attributting it to the variable that follows it, is that correct?
            – Jairo Alves
            1 hour ago











          • @JairoAlves, yes. See info bash printf or help printf within bash for details.
            – Stéphane Chazelas
            1 hour ago
















          • Thank you, very much! I'm trying it now and it will most probably do what I needed.It will help a lot! :)
            – Jairo Alves
            1 hour ago










          • If I'm understanding it correctly, the -v makes the printf command abstain from printing the result on the screen, instead attributting it to the variable that follows it, is that correct?
            – Jairo Alves
            1 hour ago











          • @JairoAlves, yes. See info bash printf or help printf within bash for details.
            – Stéphane Chazelas
            1 hour ago















          Thank you, very much! I'm trying it now and it will most probably do what I needed.It will help a lot! :)
          – Jairo Alves
          1 hour ago




          Thank you, very much! I'm trying it now and it will most probably do what I needed.It will help a lot! :)
          – Jairo Alves
          1 hour ago












          If I'm understanding it correctly, the -v makes the printf command abstain from printing the result on the screen, instead attributting it to the variable that follows it, is that correct?
          – Jairo Alves
          1 hour ago





          If I'm understanding it correctly, the -v makes the printf command abstain from printing the result on the screen, instead attributting it to the variable that follows it, is that correct?
          – Jairo Alves
          1 hour ago













          @JairoAlves, yes. See info bash printf or help printf within bash for details.
          – Stéphane Chazelas
          1 hour ago




          @JairoAlves, yes. See info bash printf or help printf within bash for details.
          – Stéphane Chazelas
          1 hour ago










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









           

          draft saved


          draft discarded


















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












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











          Jairo Alves 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%2f480170%2fhow-to-align-a-string-variable-in-bash%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Peggy Mitchell

          Palaiologos

          The Forum (Inglewood, California)