Search code examples
linuxbashterminalascii-art

Bash script for printing Heart shape


i want to write a bash script to echo a shape(heart) that made of strings and draw it in Terminal. like this picture: i find a bash script but it didn't work.

Picture

echo '\x1f\x8b\x08\x00\x95\x10\xe0R\x02\xffSPP\xf0\xc9/KU\x80\x03\x10\x8f\x0bB\xa1c.l\x82dJ\xe0\xb0\x01\xe6\x02\x0cATa.T\xf7\x02\x00\xd9\x91g\x05\xc5\x00\x00\x00'|gunzip

i find above code and tried it, but it didn't work.


Solution

  • Background:

    Someone created a text file with the textual image of the heart; let's call the file heart.

    Then they (likely) zipped the file with gzip, eg: gzip heart which generated the file heart.gz.

    At this point if you ran the file through gunzip and dumped the results to stdout you'd get:

    $ gunzip -c heart.gz
       Love          Love
      LoveLoveLoveLoveLove
    LoveLoveLoveLoveLoveLove
    LoveLoveLoveLoveLoveLove
    LoveLoveLoveLoveLoveLove
      LoveLoveLoveLoveLove
        LoveLoveLoveLove
          LoveLoveLove
              Love
    

    You could also generate the same results by cating the heart.gz and then piping the output to gunzip, eg:

    $ cat heart.gz | gunzip
       Love          Love
      LoveLoveLoveLoveLove
    LoveLoveLoveLoveLoveLove
    LoveLoveLoveLoveLoveLove
    LoveLoveLoveLoveLoveLove
      LoveLoveLoveLoveLove
        LoveLoveLoveLove
          LoveLoveLove
              Love
    

    Current question:

    Instead of uploading the heart.gz file and then requiring the download of said file, the originator (in essence) converted heart.gz to its equivalent string of hex codes. This eliminated the need to upload the heart.gz file.

    Now, intead of running the file (heart.gz) through gunzip, the user can echo the hex string to stdout (basically generate the same output as cat heart.gz), which is then piped to gunzip, with the net result that the command you're asking about should generate the image in question:

    $ echo '\x1f\x8b\x08\x00\x95\x10\xe0R\x02\xffSPP\xf0\xc9/KU\x80\x03\x10\x8f\x0bB\xa1c.l\x82dJ\xe0\xb0\x01\xe6\x02\x0cATa.T\xf7\x02\x00\xd9\x91g\x05\xc5\x00\x00\x00'|gunzip
       Love          Love
      LoveLoveLoveLoveLove
    LoveLoveLoveLoveLoveLove
    LoveLoveLoveLoveLoveLove
    LoveLoveLoveLoveLoveLove
      LoveLoveLoveLoveLove
        LoveLoveLoveLove
          LoveLoveLove
              Love
    

    NOTE: echo may not work exactly right depending on your version of echo, so you may need to use echo -e (as per Cyrus comment) or replace echo with printf (as per that other guy's answer).