bash script , echo output in box

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











up vote
5
down vote

favorite
7












I have created this Function , which generate below output on terminal , but this function seems complex, So I posted here for any improvement or for same alternate solution.
enter image description here



#!/bin/bash
function box_out() ' ; echo -n "$space" ; printf "%sn" '

box_out $@









share|improve this question

























    up vote
    5
    down vote

    favorite
    7












    I have created this Function , which generate below output on terminal , but this function seems complex, So I posted here for any improvement or for same alternate solution.
    enter image description here



    #!/bin/bash
    function box_out() ' ; echo -n "$space" ; printf "%sn" '

    box_out $@









    share|improve this question























      up vote
      5
      down vote

      favorite
      7









      up vote
      5
      down vote

      favorite
      7






      7





      I have created this Function , which generate below output on terminal , but this function seems complex, So I posted here for any improvement or for same alternate solution.
      enter image description here



      #!/bin/bash
      function box_out() ' ; echo -n "$space" ; printf "%sn" '

      box_out $@









      share|improve this question













      I have created this Function , which generate below output on terminal , but this function seems complex, So I posted here for any improvement or for same alternate solution.
      enter image description here



      #!/bin/bash
      function box_out() ' ; echo -n "$space" ; printf "%sn" '

      box_out $@






      printf tput bash-script






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 30 '13 at 15:08









      Rahul Patil

      14.2k185882




      14.2k185882




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          12
          down vote



          accepted










          As your shebang and syntax indicates unportable bash, I prefer it this way:



          function box_out()




          Of course, you can optimize it if you wish.



          Update as requested in comment, to handle multiline text too.



          function box_out()

          local s=("$@") b w
          for l in "$s[@]"; do
          ((w<$#l)) && b="$l"; w="$#l";
          done
          tput setaf 3
          echo " -$b//?/--



          Call it with multiple parameters, like box_out 'first line' 'more line' 'even more line'.






          share|improve this answer


















          • 2




            Awesome.............. You are Master ....
            – Rahul Patil
            Mar 31 '13 at 8:29










          • How would I apply this function to multiple lines, or a single line with n type CRs?
            – TryTryAgain
            Jun 2 '16 at 17:48










          • @manatwork that's marvelous, BUT... it's not working for expanding variables...and then if I try any quoting to resolve, it prints on one line again. Thank you so much for the quick response and brilliant solution!
            – TryTryAgain
            Jun 2 '16 at 19:17






          • 1




            Variable expansion should occur before the box_out function being executed. Double quotes and escapes should still work as usual: pastebin.com/ekmUKUkn By the way, you also use bash, right?
            – manatwork
            Jun 3 '16 at 7:20






          • 1




            @AndreasStorvikStrauman: $s[@] – all elements of array s; ((w<$#l)) – ((..)) shell arithmetic testing whether variable w's value is less than variable l value's length, obtained with parameter expansion (# – string length); $b//?/- – parameter expansion replacing all occurrences of any character with a “-” (// – replace all; ? – wildcard meaning any character).
            – manatwork
            Apr 7 at 17:31

















          up vote
          3
          down vote













          Boxes!



          $ echo 'Love Unix & Linux' | boxes -d stone -p a2v1
          +---------------------+
          | |
          | Love Unix & Linux |
          | |
          +---------------------+


          Since boxes requires text to be sent to it as a file, I recommend using the syntax in the above example where the text is piped into boxes through echo.



          "Why boxes?"



          Using it is easy. Just tell it what border design you want to use and how you want it to appear, and you're done.



          Of course if you want to be creative, you can make your own designs. It's really easy and fun to do. The only drawback with using boxes is that I haven't figured out how to center a box generated with boxes to align center with the screen, though there is a bash hack for that



          "What about color?"



          The original question had demonstrated the use of color codes. So it only seems right to show how boxes would handle this.



          While tput is popular, I consider myself an old school Bash person and still like using the escape commands. I'm sure there are a few folks here at StackExchange that would argue against it, but to each their own. Regardless, I am willing to set aside my personal preference of doing this to include another example doing it the tput way.



          Naturally, I think the first step is to set the color of the inside text. So let's do that first.



          printf "$(tput setaf 4)Love Unix & Linux$(tput sgr 0)" | boxes -d stone -p a2v1
          +---------------------------------+
          | |
          | Love Unix & Linux |
          | |
          +---------------------------------+


          If this were in the terminal, Love Unix & Linux would be blue...however as you can see, boxes did not handle this well. So what wen't wrong?



          printf "$(tput setaf 4)Love Unix & Linux$(tput sgr 0)" | boxes -d stone -p a2v1 | cat -A
          +---------------------------------+$
          | |$
          | ^[[34mLove Unix & Linux^[(B^[[0m |$
          | |$
          +---------------------------------+$


          A closer inspection by showing the hidden characters with cat -A shows boxes assumed the length of the box to include the length of text AND the escape characters.



          It should be noted though, that if you use a program like lolcat outside of boxes, the output looks like this



          $ echo 'Love Unix & Linux' | boxes -d stone -p a2v1 | lolcat -f
          +---------------------+
          | |
          | Love Unix & Linux |
          | |
          +---------------------+


          but with the box and text in rainbow colors.



          It's a good thing I hadn't made my own border designs that included color codes in them as of yet, as I would imagine the border designs would be fallable to the same problems.



          Centering Text, ASCII Art, and BOXES!!!



          The other flaw boxes has is that if you know how to use Bash to center text to the with of the terminal, boxes will still align the box to the left of the screen. Figured it out.



          When you want to center text that is not in a box, you can simply use



          center()
          COLS=$(tput cols) # use the current width of the terminal.
          printf "%*sn" "$((($#1+$COLS)/2))" "$1"



          And use that to center a single line of text.



          center 'Love Unix & Linux'


          For ascii-art where multiple lines are used and must be fixed in place there is this option.



          # Rather than removing text, even things out by padding lines with spaces
          draw_banner() sed -n -e 's/^ / /g;p')
          line=$(printf "%-$BWs" "$line")
          center "$line" # our center function from earlier.
          done < "$banner"


          draw_banner "path/to/your_ascii_art_logo.txt"


          But if you like to use things like figlet, you don't need to use those functions as the -c option provides an option to center.



          $figfontdir="path/to/figlet/fonts"
          $figfont="Alligator"
          text_banner()
          COLS=$(tput cols) # use the width of the terminal!
          figlet -d "$figfontdir" -f "$figfont" -k -w $COLS -c "$1"


          text_banner 'Love Unix & Linux'


          For boxes, we do something similar to draw_banner() but we need to pipe the data!



          # Center a box created with `boxes
          # It's like draw_banner, but `<<<` reads a string of data rather than a file.
          $boxfile="/path/to/your_box_designs.box" # or ".txt". I like ".box".
          $boxdesgin="stone"
          center_box() awk 'print length'

          (
          # A bunch of stuff to center!
          ) | boxes -f $boxfile -d $boxdesign -a hcvcjc | center_box


          Outstanding issues



          Fixing both of these issues the UTF/ANSI character issue would not only make boxes a better solution to encapsulating text in an ASCII box, but allow for a creative alternative that could be called upon instead of hand-coding boxes.






          share|improve this answer






















          • One other thing I should mention: If you install lolcat DO NOT install it through apt-get, install it as a gem through Ruby. The Ruby gem version is newer and respects ANSI box characters.
            – JRCharney
            Sep 9 at 5:07


















          up vote
          2
          down vote













          So, my solution is not quite the same as yours, but strictly speaking it prints a box around the text, and the implementation is a bit simpler so I thought I'd share.



          banner() sed 's/./#/g')
          echo "$edge"
          echo "$msg"
          echo "$edge"



          And here it is in action:



          $ banner "hi"
          ######
          # hi #
          ######
          $ banner "hi there"
          ############
          # hi there #
          ############


          Just plain text, no fancy ansi colors or anything.






          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: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            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%2f70615%2fbash-script-echo-output-in-box%23new-answer', 'question_page');

            );

            Post as a guest






























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            12
            down vote



            accepted










            As your shebang and syntax indicates unportable bash, I prefer it this way:



            function box_out()




            Of course, you can optimize it if you wish.



            Update as requested in comment, to handle multiline text too.



            function box_out()

            local s=("$@") b w
            for l in "$s[@]"; do
            ((w<$#l)) && b="$l"; w="$#l";
            done
            tput setaf 3
            echo " -$b//?/--



            Call it with multiple parameters, like box_out 'first line' 'more line' 'even more line'.






            share|improve this answer


















            • 2




              Awesome.............. You are Master ....
              – Rahul Patil
              Mar 31 '13 at 8:29










            • How would I apply this function to multiple lines, or a single line with n type CRs?
              – TryTryAgain
              Jun 2 '16 at 17:48










            • @manatwork that's marvelous, BUT... it's not working for expanding variables...and then if I try any quoting to resolve, it prints on one line again. Thank you so much for the quick response and brilliant solution!
              – TryTryAgain
              Jun 2 '16 at 19:17






            • 1




              Variable expansion should occur before the box_out function being executed. Double quotes and escapes should still work as usual: pastebin.com/ekmUKUkn By the way, you also use bash, right?
              – manatwork
              Jun 3 '16 at 7:20






            • 1




              @AndreasStorvikStrauman: $s[@] – all elements of array s; ((w<$#l)) – ((..)) shell arithmetic testing whether variable w's value is less than variable l value's length, obtained with parameter expansion (# – string length); $b//?/- – parameter expansion replacing all occurrences of any character with a “-” (// – replace all; ? – wildcard meaning any character).
              – manatwork
              Apr 7 at 17:31














            up vote
            12
            down vote



            accepted










            As your shebang and syntax indicates unportable bash, I prefer it this way:



            function box_out()




            Of course, you can optimize it if you wish.



            Update as requested in comment, to handle multiline text too.



            function box_out()

            local s=("$@") b w
            for l in "$s[@]"; do
            ((w<$#l)) && b="$l"; w="$#l";
            done
            tput setaf 3
            echo " -$b//?/--



            Call it with multiple parameters, like box_out 'first line' 'more line' 'even more line'.






            share|improve this answer


















            • 2




              Awesome.............. You are Master ....
              – Rahul Patil
              Mar 31 '13 at 8:29










            • How would I apply this function to multiple lines, or a single line with n type CRs?
              – TryTryAgain
              Jun 2 '16 at 17:48










            • @manatwork that's marvelous, BUT... it's not working for expanding variables...and then if I try any quoting to resolve, it prints on one line again. Thank you so much for the quick response and brilliant solution!
              – TryTryAgain
              Jun 2 '16 at 19:17






            • 1




              Variable expansion should occur before the box_out function being executed. Double quotes and escapes should still work as usual: pastebin.com/ekmUKUkn By the way, you also use bash, right?
              – manatwork
              Jun 3 '16 at 7:20






            • 1




              @AndreasStorvikStrauman: $s[@] – all elements of array s; ((w<$#l)) – ((..)) shell arithmetic testing whether variable w's value is less than variable l value's length, obtained with parameter expansion (# – string length); $b//?/- – parameter expansion replacing all occurrences of any character with a “-” (// – replace all; ? – wildcard meaning any character).
              – manatwork
              Apr 7 at 17:31












            up vote
            12
            down vote



            accepted







            up vote
            12
            down vote



            accepted






            As your shebang and syntax indicates unportable bash, I prefer it this way:



            function box_out()




            Of course, you can optimize it if you wish.



            Update as requested in comment, to handle multiline text too.



            function box_out()

            local s=("$@") b w
            for l in "$s[@]"; do
            ((w<$#l)) && b="$l"; w="$#l";
            done
            tput setaf 3
            echo " -$b//?/--



            Call it with multiple parameters, like box_out 'first line' 'more line' 'even more line'.






            share|improve this answer














            As your shebang and syntax indicates unportable bash, I prefer it this way:



            function box_out()




            Of course, you can optimize it if you wish.



            Update as requested in comment, to handle multiline text too.



            function box_out()

            local s=("$@") b w
            for l in "$s[@]"; do
            ((w<$#l)) && b="$l"; w="$#l";
            done
            tput setaf 3
            echo " -$b//?/--



            Call it with multiple parameters, like box_out 'first line' 'more line' 'even more line'.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jun 2 '16 at 18:20

























            answered Mar 30 '13 at 15:35









            manatwork

            21.1k38184




            21.1k38184







            • 2




              Awesome.............. You are Master ....
              – Rahul Patil
              Mar 31 '13 at 8:29










            • How would I apply this function to multiple lines, or a single line with n type CRs?
              – TryTryAgain
              Jun 2 '16 at 17:48










            • @manatwork that's marvelous, BUT... it's not working for expanding variables...and then if I try any quoting to resolve, it prints on one line again. Thank you so much for the quick response and brilliant solution!
              – TryTryAgain
              Jun 2 '16 at 19:17






            • 1




              Variable expansion should occur before the box_out function being executed. Double quotes and escapes should still work as usual: pastebin.com/ekmUKUkn By the way, you also use bash, right?
              – manatwork
              Jun 3 '16 at 7:20






            • 1




              @AndreasStorvikStrauman: $s[@] – all elements of array s; ((w<$#l)) – ((..)) shell arithmetic testing whether variable w's value is less than variable l value's length, obtained with parameter expansion (# – string length); $b//?/- – parameter expansion replacing all occurrences of any character with a “-” (// – replace all; ? – wildcard meaning any character).
              – manatwork
              Apr 7 at 17:31












            • 2




              Awesome.............. You are Master ....
              – Rahul Patil
              Mar 31 '13 at 8:29










            • How would I apply this function to multiple lines, or a single line with n type CRs?
              – TryTryAgain
              Jun 2 '16 at 17:48










            • @manatwork that's marvelous, BUT... it's not working for expanding variables...and then if I try any quoting to resolve, it prints on one line again. Thank you so much for the quick response and brilliant solution!
              – TryTryAgain
              Jun 2 '16 at 19:17






            • 1




              Variable expansion should occur before the box_out function being executed. Double quotes and escapes should still work as usual: pastebin.com/ekmUKUkn By the way, you also use bash, right?
              – manatwork
              Jun 3 '16 at 7:20






            • 1




              @AndreasStorvikStrauman: $s[@] – all elements of array s; ((w<$#l)) – ((..)) shell arithmetic testing whether variable w's value is less than variable l value's length, obtained with parameter expansion (# – string length); $b//?/- – parameter expansion replacing all occurrences of any character with a “-” (// – replace all; ? – wildcard meaning any character).
              – manatwork
              Apr 7 at 17:31







            2




            2




            Awesome.............. You are Master ....
            – Rahul Patil
            Mar 31 '13 at 8:29




            Awesome.............. You are Master ....
            – Rahul Patil
            Mar 31 '13 at 8:29












            How would I apply this function to multiple lines, or a single line with n type CRs?
            – TryTryAgain
            Jun 2 '16 at 17:48




            How would I apply this function to multiple lines, or a single line with n type CRs?
            – TryTryAgain
            Jun 2 '16 at 17:48












            @manatwork that's marvelous, BUT... it's not working for expanding variables...and then if I try any quoting to resolve, it prints on one line again. Thank you so much for the quick response and brilliant solution!
            – TryTryAgain
            Jun 2 '16 at 19:17




            @manatwork that's marvelous, BUT... it's not working for expanding variables...and then if I try any quoting to resolve, it prints on one line again. Thank you so much for the quick response and brilliant solution!
            – TryTryAgain
            Jun 2 '16 at 19:17




            1




            1




            Variable expansion should occur before the box_out function being executed. Double quotes and escapes should still work as usual: pastebin.com/ekmUKUkn By the way, you also use bash, right?
            – manatwork
            Jun 3 '16 at 7:20




            Variable expansion should occur before the box_out function being executed. Double quotes and escapes should still work as usual: pastebin.com/ekmUKUkn By the way, you also use bash, right?
            – manatwork
            Jun 3 '16 at 7:20




            1




            1




            @AndreasStorvikStrauman: $s[@] – all elements of array s; ((w<$#l)) – ((..)) shell arithmetic testing whether variable w's value is less than variable l value's length, obtained with parameter expansion (# – string length); $b//?/- – parameter expansion replacing all occurrences of any character with a “-” (// – replace all; ? – wildcard meaning any character).
            – manatwork
            Apr 7 at 17:31




            @AndreasStorvikStrauman: $s[@] – all elements of array s; ((w<$#l)) – ((..)) shell arithmetic testing whether variable w's value is less than variable l value's length, obtained with parameter expansion (# – string length); $b//?/- – parameter expansion replacing all occurrences of any character with a “-” (// – replace all; ? – wildcard meaning any character).
            – manatwork
            Apr 7 at 17:31












            up vote
            3
            down vote













            Boxes!



            $ echo 'Love Unix & Linux' | boxes -d stone -p a2v1
            +---------------------+
            | |
            | Love Unix & Linux |
            | |
            +---------------------+


            Since boxes requires text to be sent to it as a file, I recommend using the syntax in the above example where the text is piped into boxes through echo.



            "Why boxes?"



            Using it is easy. Just tell it what border design you want to use and how you want it to appear, and you're done.



            Of course if you want to be creative, you can make your own designs. It's really easy and fun to do. The only drawback with using boxes is that I haven't figured out how to center a box generated with boxes to align center with the screen, though there is a bash hack for that



            "What about color?"



            The original question had demonstrated the use of color codes. So it only seems right to show how boxes would handle this.



            While tput is popular, I consider myself an old school Bash person and still like using the escape commands. I'm sure there are a few folks here at StackExchange that would argue against it, but to each their own. Regardless, I am willing to set aside my personal preference of doing this to include another example doing it the tput way.



            Naturally, I think the first step is to set the color of the inside text. So let's do that first.



            printf "$(tput setaf 4)Love Unix & Linux$(tput sgr 0)" | boxes -d stone -p a2v1
            +---------------------------------+
            | |
            | Love Unix & Linux |
            | |
            +---------------------------------+


            If this were in the terminal, Love Unix & Linux would be blue...however as you can see, boxes did not handle this well. So what wen't wrong?



            printf "$(tput setaf 4)Love Unix & Linux$(tput sgr 0)" | boxes -d stone -p a2v1 | cat -A
            +---------------------------------+$
            | |$
            | ^[[34mLove Unix & Linux^[(B^[[0m |$
            | |$
            +---------------------------------+$


            A closer inspection by showing the hidden characters with cat -A shows boxes assumed the length of the box to include the length of text AND the escape characters.



            It should be noted though, that if you use a program like lolcat outside of boxes, the output looks like this



            $ echo 'Love Unix & Linux' | boxes -d stone -p a2v1 | lolcat -f
            +---------------------+
            | |
            | Love Unix & Linux |
            | |
            +---------------------+


            but with the box and text in rainbow colors.



            It's a good thing I hadn't made my own border designs that included color codes in them as of yet, as I would imagine the border designs would be fallable to the same problems.



            Centering Text, ASCII Art, and BOXES!!!



            The other flaw boxes has is that if you know how to use Bash to center text to the with of the terminal, boxes will still align the box to the left of the screen. Figured it out.



            When you want to center text that is not in a box, you can simply use



            center()
            COLS=$(tput cols) # use the current width of the terminal.
            printf "%*sn" "$((($#1+$COLS)/2))" "$1"



            And use that to center a single line of text.



            center 'Love Unix & Linux'


            For ascii-art where multiple lines are used and must be fixed in place there is this option.



            # Rather than removing text, even things out by padding lines with spaces
            draw_banner() sed -n -e 's/^ / /g;p')
            line=$(printf "%-$BWs" "$line")
            center "$line" # our center function from earlier.
            done < "$banner"


            draw_banner "path/to/your_ascii_art_logo.txt"


            But if you like to use things like figlet, you don't need to use those functions as the -c option provides an option to center.



            $figfontdir="path/to/figlet/fonts"
            $figfont="Alligator"
            text_banner()
            COLS=$(tput cols) # use the width of the terminal!
            figlet -d "$figfontdir" -f "$figfont" -k -w $COLS -c "$1"


            text_banner 'Love Unix & Linux'


            For boxes, we do something similar to draw_banner() but we need to pipe the data!



            # Center a box created with `boxes
            # It's like draw_banner, but `<<<` reads a string of data rather than a file.
            $boxfile="/path/to/your_box_designs.box" # or ".txt". I like ".box".
            $boxdesgin="stone"
            center_box() awk 'print length'

            (
            # A bunch of stuff to center!
            ) | boxes -f $boxfile -d $boxdesign -a hcvcjc | center_box


            Outstanding issues



            Fixing both of these issues the UTF/ANSI character issue would not only make boxes a better solution to encapsulating text in an ASCII box, but allow for a creative alternative that could be called upon instead of hand-coding boxes.






            share|improve this answer






















            • One other thing I should mention: If you install lolcat DO NOT install it through apt-get, install it as a gem through Ruby. The Ruby gem version is newer and respects ANSI box characters.
              – JRCharney
              Sep 9 at 5:07















            up vote
            3
            down vote













            Boxes!



            $ echo 'Love Unix & Linux' | boxes -d stone -p a2v1
            +---------------------+
            | |
            | Love Unix & Linux |
            | |
            +---------------------+


            Since boxes requires text to be sent to it as a file, I recommend using the syntax in the above example where the text is piped into boxes through echo.



            "Why boxes?"



            Using it is easy. Just tell it what border design you want to use and how you want it to appear, and you're done.



            Of course if you want to be creative, you can make your own designs. It's really easy and fun to do. The only drawback with using boxes is that I haven't figured out how to center a box generated with boxes to align center with the screen, though there is a bash hack for that



            "What about color?"



            The original question had demonstrated the use of color codes. So it only seems right to show how boxes would handle this.



            While tput is popular, I consider myself an old school Bash person and still like using the escape commands. I'm sure there are a few folks here at StackExchange that would argue against it, but to each their own. Regardless, I am willing to set aside my personal preference of doing this to include another example doing it the tput way.



            Naturally, I think the first step is to set the color of the inside text. So let's do that first.



            printf "$(tput setaf 4)Love Unix & Linux$(tput sgr 0)" | boxes -d stone -p a2v1
            +---------------------------------+
            | |
            | Love Unix & Linux |
            | |
            +---------------------------------+


            If this were in the terminal, Love Unix & Linux would be blue...however as you can see, boxes did not handle this well. So what wen't wrong?



            printf "$(tput setaf 4)Love Unix & Linux$(tput sgr 0)" | boxes -d stone -p a2v1 | cat -A
            +---------------------------------+$
            | |$
            | ^[[34mLove Unix & Linux^[(B^[[0m |$
            | |$
            +---------------------------------+$


            A closer inspection by showing the hidden characters with cat -A shows boxes assumed the length of the box to include the length of text AND the escape characters.



            It should be noted though, that if you use a program like lolcat outside of boxes, the output looks like this



            $ echo 'Love Unix & Linux' | boxes -d stone -p a2v1 | lolcat -f
            +---------------------+
            | |
            | Love Unix & Linux |
            | |
            +---------------------+


            but with the box and text in rainbow colors.



            It's a good thing I hadn't made my own border designs that included color codes in them as of yet, as I would imagine the border designs would be fallable to the same problems.



            Centering Text, ASCII Art, and BOXES!!!



            The other flaw boxes has is that if you know how to use Bash to center text to the with of the terminal, boxes will still align the box to the left of the screen. Figured it out.



            When you want to center text that is not in a box, you can simply use



            center()
            COLS=$(tput cols) # use the current width of the terminal.
            printf "%*sn" "$((($#1+$COLS)/2))" "$1"



            And use that to center a single line of text.



            center 'Love Unix & Linux'


            For ascii-art where multiple lines are used and must be fixed in place there is this option.



            # Rather than removing text, even things out by padding lines with spaces
            draw_banner() sed -n -e 's/^ / /g;p')
            line=$(printf "%-$BWs" "$line")
            center "$line" # our center function from earlier.
            done < "$banner"


            draw_banner "path/to/your_ascii_art_logo.txt"


            But if you like to use things like figlet, you don't need to use those functions as the -c option provides an option to center.



            $figfontdir="path/to/figlet/fonts"
            $figfont="Alligator"
            text_banner()
            COLS=$(tput cols) # use the width of the terminal!
            figlet -d "$figfontdir" -f "$figfont" -k -w $COLS -c "$1"


            text_banner 'Love Unix & Linux'


            For boxes, we do something similar to draw_banner() but we need to pipe the data!



            # Center a box created with `boxes
            # It's like draw_banner, but `<<<` reads a string of data rather than a file.
            $boxfile="/path/to/your_box_designs.box" # or ".txt". I like ".box".
            $boxdesgin="stone"
            center_box() awk 'print length'

            (
            # A bunch of stuff to center!
            ) | boxes -f $boxfile -d $boxdesign -a hcvcjc | center_box


            Outstanding issues



            Fixing both of these issues the UTF/ANSI character issue would not only make boxes a better solution to encapsulating text in an ASCII box, but allow for a creative alternative that could be called upon instead of hand-coding boxes.






            share|improve this answer






















            • One other thing I should mention: If you install lolcat DO NOT install it through apt-get, install it as a gem through Ruby. The Ruby gem version is newer and respects ANSI box characters.
              – JRCharney
              Sep 9 at 5:07













            up vote
            3
            down vote










            up vote
            3
            down vote









            Boxes!



            $ echo 'Love Unix & Linux' | boxes -d stone -p a2v1
            +---------------------+
            | |
            | Love Unix & Linux |
            | |
            +---------------------+


            Since boxes requires text to be sent to it as a file, I recommend using the syntax in the above example where the text is piped into boxes through echo.



            "Why boxes?"



            Using it is easy. Just tell it what border design you want to use and how you want it to appear, and you're done.



            Of course if you want to be creative, you can make your own designs. It's really easy and fun to do. The only drawback with using boxes is that I haven't figured out how to center a box generated with boxes to align center with the screen, though there is a bash hack for that



            "What about color?"



            The original question had demonstrated the use of color codes. So it only seems right to show how boxes would handle this.



            While tput is popular, I consider myself an old school Bash person and still like using the escape commands. I'm sure there are a few folks here at StackExchange that would argue against it, but to each their own. Regardless, I am willing to set aside my personal preference of doing this to include another example doing it the tput way.



            Naturally, I think the first step is to set the color of the inside text. So let's do that first.



            printf "$(tput setaf 4)Love Unix & Linux$(tput sgr 0)" | boxes -d stone -p a2v1
            +---------------------------------+
            | |
            | Love Unix & Linux |
            | |
            +---------------------------------+


            If this were in the terminal, Love Unix & Linux would be blue...however as you can see, boxes did not handle this well. So what wen't wrong?



            printf "$(tput setaf 4)Love Unix & Linux$(tput sgr 0)" | boxes -d stone -p a2v1 | cat -A
            +---------------------------------+$
            | |$
            | ^[[34mLove Unix & Linux^[(B^[[0m |$
            | |$
            +---------------------------------+$


            A closer inspection by showing the hidden characters with cat -A shows boxes assumed the length of the box to include the length of text AND the escape characters.



            It should be noted though, that if you use a program like lolcat outside of boxes, the output looks like this



            $ echo 'Love Unix & Linux' | boxes -d stone -p a2v1 | lolcat -f
            +---------------------+
            | |
            | Love Unix & Linux |
            | |
            +---------------------+


            but with the box and text in rainbow colors.



            It's a good thing I hadn't made my own border designs that included color codes in them as of yet, as I would imagine the border designs would be fallable to the same problems.



            Centering Text, ASCII Art, and BOXES!!!



            The other flaw boxes has is that if you know how to use Bash to center text to the with of the terminal, boxes will still align the box to the left of the screen. Figured it out.



            When you want to center text that is not in a box, you can simply use



            center()
            COLS=$(tput cols) # use the current width of the terminal.
            printf "%*sn" "$((($#1+$COLS)/2))" "$1"



            And use that to center a single line of text.



            center 'Love Unix & Linux'


            For ascii-art where multiple lines are used and must be fixed in place there is this option.



            # Rather than removing text, even things out by padding lines with spaces
            draw_banner() sed -n -e 's/^ / /g;p')
            line=$(printf "%-$BWs" "$line")
            center "$line" # our center function from earlier.
            done < "$banner"


            draw_banner "path/to/your_ascii_art_logo.txt"


            But if you like to use things like figlet, you don't need to use those functions as the -c option provides an option to center.



            $figfontdir="path/to/figlet/fonts"
            $figfont="Alligator"
            text_banner()
            COLS=$(tput cols) # use the width of the terminal!
            figlet -d "$figfontdir" -f "$figfont" -k -w $COLS -c "$1"


            text_banner 'Love Unix & Linux'


            For boxes, we do something similar to draw_banner() but we need to pipe the data!



            # Center a box created with `boxes
            # It's like draw_banner, but `<<<` reads a string of data rather than a file.
            $boxfile="/path/to/your_box_designs.box" # or ".txt". I like ".box".
            $boxdesgin="stone"
            center_box() awk 'print length'

            (
            # A bunch of stuff to center!
            ) | boxes -f $boxfile -d $boxdesign -a hcvcjc | center_box


            Outstanding issues



            Fixing both of these issues the UTF/ANSI character issue would not only make boxes a better solution to encapsulating text in an ASCII box, but allow for a creative alternative that could be called upon instead of hand-coding boxes.






            share|improve this answer














            Boxes!



            $ echo 'Love Unix & Linux' | boxes -d stone -p a2v1
            +---------------------+
            | |
            | Love Unix & Linux |
            | |
            +---------------------+


            Since boxes requires text to be sent to it as a file, I recommend using the syntax in the above example where the text is piped into boxes through echo.



            "Why boxes?"



            Using it is easy. Just tell it what border design you want to use and how you want it to appear, and you're done.



            Of course if you want to be creative, you can make your own designs. It's really easy and fun to do. The only drawback with using boxes is that I haven't figured out how to center a box generated with boxes to align center with the screen, though there is a bash hack for that



            "What about color?"



            The original question had demonstrated the use of color codes. So it only seems right to show how boxes would handle this.



            While tput is popular, I consider myself an old school Bash person and still like using the escape commands. I'm sure there are a few folks here at StackExchange that would argue against it, but to each their own. Regardless, I am willing to set aside my personal preference of doing this to include another example doing it the tput way.



            Naturally, I think the first step is to set the color of the inside text. So let's do that first.



            printf "$(tput setaf 4)Love Unix & Linux$(tput sgr 0)" | boxes -d stone -p a2v1
            +---------------------------------+
            | |
            | Love Unix & Linux |
            | |
            +---------------------------------+


            If this were in the terminal, Love Unix & Linux would be blue...however as you can see, boxes did not handle this well. So what wen't wrong?



            printf "$(tput setaf 4)Love Unix & Linux$(tput sgr 0)" | boxes -d stone -p a2v1 | cat -A
            +---------------------------------+$
            | |$
            | ^[[34mLove Unix & Linux^[(B^[[0m |$
            | |$
            +---------------------------------+$


            A closer inspection by showing the hidden characters with cat -A shows boxes assumed the length of the box to include the length of text AND the escape characters.



            It should be noted though, that if you use a program like lolcat outside of boxes, the output looks like this



            $ echo 'Love Unix & Linux' | boxes -d stone -p a2v1 | lolcat -f
            +---------------------+
            | |
            | Love Unix & Linux |
            | |
            +---------------------+


            but with the box and text in rainbow colors.



            It's a good thing I hadn't made my own border designs that included color codes in them as of yet, as I would imagine the border designs would be fallable to the same problems.



            Centering Text, ASCII Art, and BOXES!!!



            The other flaw boxes has is that if you know how to use Bash to center text to the with of the terminal, boxes will still align the box to the left of the screen. Figured it out.



            When you want to center text that is not in a box, you can simply use



            center()
            COLS=$(tput cols) # use the current width of the terminal.
            printf "%*sn" "$((($#1+$COLS)/2))" "$1"



            And use that to center a single line of text.



            center 'Love Unix & Linux'


            For ascii-art where multiple lines are used and must be fixed in place there is this option.



            # Rather than removing text, even things out by padding lines with spaces
            draw_banner() sed -n -e 's/^ / /g;p')
            line=$(printf "%-$BWs" "$line")
            center "$line" # our center function from earlier.
            done < "$banner"


            draw_banner "path/to/your_ascii_art_logo.txt"


            But if you like to use things like figlet, you don't need to use those functions as the -c option provides an option to center.



            $figfontdir="path/to/figlet/fonts"
            $figfont="Alligator"
            text_banner()
            COLS=$(tput cols) # use the width of the terminal!
            figlet -d "$figfontdir" -f "$figfont" -k -w $COLS -c "$1"


            text_banner 'Love Unix & Linux'


            For boxes, we do something similar to draw_banner() but we need to pipe the data!



            # Center a box created with `boxes
            # It's like draw_banner, but `<<<` reads a string of data rather than a file.
            $boxfile="/path/to/your_box_designs.box" # or ".txt". I like ".box".
            $boxdesgin="stone"
            center_box() awk 'print length'

            (
            # A bunch of stuff to center!
            ) | boxes -f $boxfile -d $boxdesign -a hcvcjc | center_box


            Outstanding issues



            Fixing both of these issues the UTF/ANSI character issue would not only make boxes a better solution to encapsulating text in an ASCII box, but allow for a creative alternative that could be called upon instead of hand-coding boxes.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 24 at 19:59

























            answered Aug 23 at 15:34









            JRCharney

            312




            312











            • One other thing I should mention: If you install lolcat DO NOT install it through apt-get, install it as a gem through Ruby. The Ruby gem version is newer and respects ANSI box characters.
              – JRCharney
              Sep 9 at 5:07

















            • One other thing I should mention: If you install lolcat DO NOT install it through apt-get, install it as a gem through Ruby. The Ruby gem version is newer and respects ANSI box characters.
              – JRCharney
              Sep 9 at 5:07
















            One other thing I should mention: If you install lolcat DO NOT install it through apt-get, install it as a gem through Ruby. The Ruby gem version is newer and respects ANSI box characters.
            – JRCharney
            Sep 9 at 5:07





            One other thing I should mention: If you install lolcat DO NOT install it through apt-get, install it as a gem through Ruby. The Ruby gem version is newer and respects ANSI box characters.
            – JRCharney
            Sep 9 at 5:07











            up vote
            2
            down vote













            So, my solution is not quite the same as yours, but strictly speaking it prints a box around the text, and the implementation is a bit simpler so I thought I'd share.



            banner() sed 's/./#/g')
            echo "$edge"
            echo "$msg"
            echo "$edge"



            And here it is in action:



            $ banner "hi"
            ######
            # hi #
            ######
            $ banner "hi there"
            ############
            # hi there #
            ############


            Just plain text, no fancy ansi colors or anything.






            share|improve this answer
























              up vote
              2
              down vote













              So, my solution is not quite the same as yours, but strictly speaking it prints a box around the text, and the implementation is a bit simpler so I thought I'd share.



              banner() sed 's/./#/g')
              echo "$edge"
              echo "$msg"
              echo "$edge"



              And here it is in action:



              $ banner "hi"
              ######
              # hi #
              ######
              $ banner "hi there"
              ############
              # hi there #
              ############


              Just plain text, no fancy ansi colors or anything.






              share|improve this answer






















                up vote
                2
                down vote










                up vote
                2
                down vote









                So, my solution is not quite the same as yours, but strictly speaking it prints a box around the text, and the implementation is a bit simpler so I thought I'd share.



                banner() sed 's/./#/g')
                echo "$edge"
                echo "$msg"
                echo "$edge"



                And here it is in action:



                $ banner "hi"
                ######
                # hi #
                ######
                $ banner "hi there"
                ############
                # hi there #
                ############


                Just plain text, no fancy ansi colors or anything.






                share|improve this answer












                So, my solution is not quite the same as yours, but strictly speaking it prints a box around the text, and the implementation is a bit simpler so I thought I'd share.



                banner() sed 's/./#/g')
                echo "$edge"
                echo "$msg"
                echo "$edge"



                And here it is in action:



                $ banner "hi"
                ######
                # hi #
                ######
                $ banner "hi there"
                ############
                # hi there #
                ############


                Just plain text, no fancy ansi colors or anything.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 18 '15 at 1:28









                robru

                17113




                17113



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f70615%2fbash-script-echo-output-in-box%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?