1. 10

In this article, we will see how to customize our terminal prompt and add a random emoji in it.

  1.  

  2. 7

    Random emoji aren’t for me, but I suppose you could use them to indicate whether the previous command succeeded (i.e. exited with a status code of zero):

    function success_indicator() {
        if [ $? -eq 0 ] ; then
            echo "😎"
        else
            echo "💩"
        fi
    }
    
    export PS1='$(success_indicator) $ '
    

    Or for zsh:

    PROMPT='%(?:😎:💩) $ '
    
    1. 2

      Hahaha that’s a great idea!

      1. 1

        This is a super nice idea! Do you mind if I add it as an extra bonus point? Of course I’ll give you credit :)

        1. 2

          Sure!

      2. 7

        Bash customizations are generally made in a file saved in the user home directory. This file is generally called .bashrc (more common on Linux systems) or .bash_profile (more common on MacOS).

        No, these files are not related to your platform. .bash_profile is executed for login shells, and .bashrc is executed for non-login shells

        1. 3

          According to this[0], MacOS runs a login shell for every invocation of the ‘terminal’ emulator. So if the desire is to ‘run this every time you open the terminal app’ then the article is showing one way (though not a portable way) to do that on MacOS.

          1. http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html
          1. 3

            Yeah, because Terminal.app defaults to --login when it executes bash, this is not really specific to MacOS and can happen on any platform.

            1. 1

              Thanks for the very good insights here! Those clarifications are great (first of all for me!) I’ll update the article shorty and add an official thank you at the end for your support!

        2. 5

          I was hoping for something that grabbed random numbers and made emoji codepoints of them… Neat trick, but it could as well be the alphabet instead of predefined emoji characters.

          1. 5

            printf -v random_maybe_emoji %b\\n "$(printf '\\U0001f%x' "$(( RANDOM % 4096 ))")"

            there, hits an emoji “most of the time”

            1. 1

              It’s the “most of the time” the issue there :P but a definitely neat trick! Thanks for sharing it :)

            2. 2

              Absolutely. I am not sure it would be so simple to pick random emojis only from the unicode ranges, but it might be worth some time for experimentation :)

              Again the goal here was only to spark some curiosity on bash customisation, not really to be comprehensive. With that being said, I am extremely willing to accept any advice on how to improve the article!

            3. 4

              I do something similar but I use a different emoji for each session. Originally, I thought it would help me keep track of which tab is which but ultimately it ended up just being a source of entertainment and a conversation starter whenever I share my screen.

              Code lives here (~/.zshrc): https://github.com/audy/dotflies/blob/master/src/zshrc#L65-L133

              1. 5

                I’m not sure that the linux tag is quite warranted here, since the article is about customizing bash on Mac OS.

                Also, it’s only about bash tricks, and not about which terminal emulators support colorful, double-width unicode characters, and why or why not.

                1. 3

                  Same for the ‘osdev’ tag, it seems like quite a stretch for that too.

                  1. 1

                    Definitely. I wasn’t sure as well but couldn’t find any other relevant tag. I was hoping for something like “bash” or “shell” or “terminal”

                  2. 1

                    The same config works also on Ubuntu (tested) and I assume in any other bash environment. I only happened to do the screenshots on a Mac. Anyway the intention of the article was to inspire some curiosity on bash customisations. It wasn’t intended to be exhaustive. With that in mind, I am more than happy to accept any suggestion on how to improve the article. Most likely it can use some constructive suggestion :)

                    1. 4

                      The article takes for granted the ability of your font and terminal emulator to display these characters properly. In my experience, that’s the “hard part”. The issues involved are at least worth a mention.

                      1. 1

                        Seems like it only works in terminals built using the VTE library too, which means others (xterm, rxvt, alacritty[?]) don’t support it.

                        1. 1

                          Most definitely. Updating the article right now. I’ll make sure to include this as well. Thank you so much!

                    2. 3

                      Is something missing from the article? The script defines SELECTED_EMOJI but never uses it, and it uses RANDOM_EMOJI without defining it. Did you intend to define a function like this?

                      function RANDOM_EMOJI() {
                          echo "${EMOJIS[$RANDOM % ${#EMOJIS[@]}]}"
                      }
                      

                      Also note that the value of SELECTED_EMOJI will not change once it’s been assigned.

                      1. 1

                        Aww… I just realized i messed up the code example there. I wanted to present a simplified version without functions and I ended up doing a broken mess. I’ll fix it shortly! Thanks so much for spotting this, it is a very bad mistake :(