Customizing Your Bash Prompt

rayalva407

Raymundo Alva

Posted on May 3, 2023

Customizing Your Bash Prompt

In Linux most of the work I do happens in a command prompt. Many people don’t really care but looking at the same bash prompt everyday was really boring to me. One day when I was pair programming with one of my peers in college I realized that their bash prompt was customized. Different font colors, special characters and even an emoji! I missed an opportunity to ask my pair programming buddy how he did that so I did some research and found out that the bash terminal can be customized in many ways! I am going to show you how to do it yourself by using my prompt as an example. Here is what mine looks like!

My Shell Prompt

For this to work we have to change the value of the PS1 environment variable. By doing this we can change the command prompt appearance and environment. To demonstrate type this into your terminal.



$ PS1="Hello Prompt "


Enter fullscreen mode Exit fullscreen mode

You will see that your prompt is now Hello Prompt.

Hello Prompt

If you open up a new terminal you will notice that your changes didn’t persist. So if we want these changes to stay the same we have to add a line with your PS1 variable to the “.bashrc” file in your home folder. If you are on Mac you have to add that line to “.bash_profile”.

Special Characters

To add dynamic information we can use sets of special characters. Here are some of the most common ones.

  • \d -Current date
  • \e - Escape character
  • \h - Hostname
  • \n - Newline
  • \t - Current time 24-hour HH:MM:SS
  • \T - 12-hour HH:MM:SS
  • \@ - 12-hour HH:MM am/pm
  • \u - Username of current user
  • \w - Path to current working directory

You are not limited to only these options though. There is a more detailed list of options you can use in the Bash Manual. So to start off with my example we would type this.



PS1="\\u@\\h: \\W\\$"


Enter fullscreen mode Exit fullscreen mode

Customized Prompt

Colors

Special characters are simple to understand. Now we move on to colors. This syntax looks very ugly and messy so you will have to double check that you typed the right thing before you save it. These are the options for the common colors.

  • Black: \033[30m
  • Blue: \033[34m
  • Cyan: \033[36m
  • Green: \033[32m
  • Magenta: \033[35m
  • Red: \033[31m
  • White: \033[37m start of sequence
  • Yellow: \033[33m

We also need to wrap the following escape sequences around our color codes.

  • \[
  • **\]**

For more information on colors you can take a look at this page.

Let’s implement some color to make the prompt look even fancier.



PS1="\\\[\\033\[35m\\\]\\u@\\h: \\W\\$ \\n\\\[\\033\[36m\\\]"


Enter fullscreen mode Exit fullscreen mode

Colorized Prompt

Emoji!

Now for the cherry on top we are going to add an emoji! I chose to add the snowman. Adding this is very easy. All you have to do is open up the emoji picker and copy and paste whatever emoji you want. On Linux press “Super key + .” or “Control + Command + Space” on Mac. In the end my PS1 variable looks like this.



PS1="\\\[\\033\[35m\\\]\\u@\\h: \\W\\$ \\n\\\[\\033\[36m\\\]// ⛄"


Enter fullscreen mode Exit fullscreen mode

Customization Completed

That is all. This might not be a tip that makes you more productive but it will sure give your work environment a personal touch. You will be happy looking at your very own personalized Bash prompt. I had a lot of fun messing with many of the options and special characters. Enjoy customizing your prompt and happy coding! 😎

💖 💪 🙅 🚩
rayalva407
Raymundo Alva

Posted on May 3, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related