Add Space Before Last Character in Variable











up vote
1
down vote

favorite












I have a variable that contains some numbers then a letter



example: 242M



I need to add a space right before the last character in that variable so that when echo'd it would look like 242 M



I've tried sed but it looks like from what I can find that only works with reading from a file and I haven't been able to get this to work modifying a variable.



Thanks!










share|improve this question







New contributor




RyanL 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 have a variable that contains some numbers then a letter



    example: 242M



    I need to add a space right before the last character in that variable so that when echo'd it would look like 242 M



    I've tried sed but it looks like from what I can find that only works with reading from a file and I haven't been able to get this to work modifying a variable.



    Thanks!










    share|improve this question







    New contributor




    RyanL 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 have a variable that contains some numbers then a letter



      example: 242M



      I need to add a space right before the last character in that variable so that when echo'd it would look like 242 M



      I've tried sed but it looks like from what I can find that only works with reading from a file and I haven't been able to get this to work modifying a variable.



      Thanks!










      share|improve this question







      New contributor




      RyanL 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 variable that contains some numbers then a letter



      example: 242M



      I need to add a space right before the last character in that variable so that when echo'd it would look like 242 M



      I've tried sed but it looks like from what I can find that only works with reading from a file and I haven't been able to get this to work modifying a variable.



      Thanks!







      bash shell-script scripting variable






      share|improve this question







      New contributor




      RyanL 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




      RyanL 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






      New contributor




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









      asked Dec 5 at 19:19









      RyanL

      82




      82




      New contributor




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





      New contributor





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






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






















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          A version that should work on any POSIX shell, not just bash:



          printf '%sn' "${a%?} ${a#"${a%?}"}"


          The extra quotes inside ${a#...} are to protect against special characters from the variable:



          $ a='10*M'; printf '%sn' "${a%?} ${a#"${a%?}"}"
          10* M


          Without them, the second variable expansion would've turned into ${a#10*}, and 10* *M would've been echoed instead.






          share|improve this answer






























            up vote
            1
            down vote













            If the other answer doesn't work (e.g. on macOS), you can also try



            a="${a%?} ${a: -1}"


            which puts together a without its last character, a space, and the last character of a.






            share|improve this answer








            New contributor




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

























              up vote
              0
              down vote













              Purely within the shell:



              var=${var:0:-1}' '${var: -1:1}


              For example:



              $ var=jeff
              $ var=${var:0:-1}' '${var: -1:1}
              $ printf '%sn' "$var"
              jef f





              share|improve this answer




























                up vote
                0
                down vote













                Here is a Sed example that works with a variable:



                q=242M
                sed 's/.$/ &/' <<eof
                $q
                eof





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


                  }
                  });






                  RyanL 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%2f486218%2fadd-space-before-last-character-in-variable%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  2
                  down vote



                  accepted










                  A version that should work on any POSIX shell, not just bash:



                  printf '%sn' "${a%?} ${a#"${a%?}"}"


                  The extra quotes inside ${a#...} are to protect against special characters from the variable:



                  $ a='10*M'; printf '%sn' "${a%?} ${a#"${a%?}"}"
                  10* M


                  Without them, the second variable expansion would've turned into ${a#10*}, and 10* *M would've been echoed instead.






                  share|improve this answer



























                    up vote
                    2
                    down vote



                    accepted










                    A version that should work on any POSIX shell, not just bash:



                    printf '%sn' "${a%?} ${a#"${a%?}"}"


                    The extra quotes inside ${a#...} are to protect against special characters from the variable:



                    $ a='10*M'; printf '%sn' "${a%?} ${a#"${a%?}"}"
                    10* M


                    Without them, the second variable expansion would've turned into ${a#10*}, and 10* *M would've been echoed instead.






                    share|improve this answer

























                      up vote
                      2
                      down vote



                      accepted







                      up vote
                      2
                      down vote



                      accepted






                      A version that should work on any POSIX shell, not just bash:



                      printf '%sn' "${a%?} ${a#"${a%?}"}"


                      The extra quotes inside ${a#...} are to protect against special characters from the variable:



                      $ a='10*M'; printf '%sn' "${a%?} ${a#"${a%?}"}"
                      10* M


                      Without them, the second variable expansion would've turned into ${a#10*}, and 10* *M would've been echoed instead.






                      share|improve this answer














                      A version that should work on any POSIX shell, not just bash:



                      printf '%sn' "${a%?} ${a#"${a%?}"}"


                      The extra quotes inside ${a#...} are to protect against special characters from the variable:



                      $ a='10*M'; printf '%sn' "${a%?} ${a#"${a%?}"}"
                      10* M


                      Without them, the second variable expansion would've turned into ${a#10*}, and 10* *M would've been echoed instead.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 5 at 22:50









                      Stéphane Chazelas

                      296k54559904




                      296k54559904










                      answered Dec 5 at 21:58









                      mosvy

                      5,1791323




                      5,1791323
























                          up vote
                          1
                          down vote













                          If the other answer doesn't work (e.g. on macOS), you can also try



                          a="${a%?} ${a: -1}"


                          which puts together a without its last character, a space, and the last character of a.






                          share|improve this answer








                          New contributor




                          Jim Danner 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













                            If the other answer doesn't work (e.g. on macOS), you can also try



                            a="${a%?} ${a: -1}"


                            which puts together a without its last character, a space, and the last character of a.






                            share|improve this answer








                            New contributor




                            Jim Danner 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










                              up vote
                              1
                              down vote









                              If the other answer doesn't work (e.g. on macOS), you can also try



                              a="${a%?} ${a: -1}"


                              which puts together a without its last character, a space, and the last character of a.






                              share|improve this answer








                              New contributor




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









                              If the other answer doesn't work (e.g. on macOS), you can also try



                              a="${a%?} ${a: -1}"


                              which puts together a without its last character, a space, and the last character of a.







                              share|improve this answer








                              New contributor




                              Jim Danner 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 answer



                              share|improve this answer






                              New contributor




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









                              answered Dec 5 at 19:32









                              Jim Danner

                              112




                              112




                              New contributor




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





                              New contributor





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






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






















                                  up vote
                                  0
                                  down vote













                                  Purely within the shell:



                                  var=${var:0:-1}' '${var: -1:1}


                                  For example:



                                  $ var=jeff
                                  $ var=${var:0:-1}' '${var: -1:1}
                                  $ printf '%sn' "$var"
                                  jef f





                                  share|improve this answer

























                                    up vote
                                    0
                                    down vote













                                    Purely within the shell:



                                    var=${var:0:-1}' '${var: -1:1}


                                    For example:



                                    $ var=jeff
                                    $ var=${var:0:-1}' '${var: -1:1}
                                    $ printf '%sn' "$var"
                                    jef f





                                    share|improve this answer























                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      Purely within the shell:



                                      var=${var:0:-1}' '${var: -1:1}


                                      For example:



                                      $ var=jeff
                                      $ var=${var:0:-1}' '${var: -1:1}
                                      $ printf '%sn' "$var"
                                      jef f





                                      share|improve this answer












                                      Purely within the shell:



                                      var=${var:0:-1}' '${var: -1:1}


                                      For example:



                                      $ var=jeff
                                      $ var=${var:0:-1}' '${var: -1:1}
                                      $ printf '%sn' "$var"
                                      jef f






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Dec 5 at 19:23









                                      Jeff Schaller

                                      37.4k1052121




                                      37.4k1052121






















                                          up vote
                                          0
                                          down vote













                                          Here is a Sed example that works with a variable:



                                          q=242M
                                          sed 's/.$/ &/' <<eof
                                          $q
                                          eof





                                          share|improve this answer

























                                            up vote
                                            0
                                            down vote













                                            Here is a Sed example that works with a variable:



                                            q=242M
                                            sed 's/.$/ &/' <<eof
                                            $q
                                            eof





                                            share|improve this answer























                                              up vote
                                              0
                                              down vote










                                              up vote
                                              0
                                              down vote









                                              Here is a Sed example that works with a variable:



                                              q=242M
                                              sed 's/.$/ &/' <<eof
                                              $q
                                              eof





                                              share|improve this answer












                                              Here is a Sed example that works with a variable:



                                              q=242M
                                              sed 's/.$/ &/' <<eof
                                              $q
                                              eof






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Dec 6 at 2:45









                                              Steven Penny

                                              2,48221737




                                              2,48221737






















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










                                                  draft saved

                                                  draft discarded


















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













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












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





                                                  Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                  Please pay close attention to the following guidance:


                                                  • 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%2f486218%2fadd-space-before-last-character-in-variable%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

                                                  Accessing regular linux commands in Huawei's Dopra Linux

                                                  Can't connect RFCOMM socket: Host is down

                                                  Kernel panic - not syncing: Fatal Exception in Interrupt