bash prompt

There's already a lot of guides out there about how to adjust your bash prompt. So this article is mostly for me to have easy access to my personal prefered prompt, no matter at which computer I am. But maybe it's useful for you as well.

Before we start ...

The setup is slightly different depending on what you are working with. I'm usually using one of two setups: Arch Linux or Windows with Gitbash. So I'll be explaining those two in this article.

General stuff

The prompt is saved in the variable PS1. The configuration goes into ~/.bashrc. To configure any styling, you need to put the desired command in escaped brackets: \[...\]

Colors

To set a font color, use the command \e[xxm, with xx being one of the following numbers:

  • 30 - black
  • 31 - red
  • 32 - green
  • 33 - yellow
  • 34 - blue
  • 35 - magenta
  • 36 - cyan
  • 37 - light grey
  • 39 - default
  • 90 - dark grey
  • 91 - light red
  • 92 - light green
  • 93 - light yellow
  • 94 - light blue
  • 95 - light magenta
  • 96 - light cyan
  • 97 - white

To set a background color, choose the desired color from above and add 10 to the color value.

Other settings

Here are some other commands, that may be useful:

  • # \e[0m - reset all
  • # \e[1m \e[21m - bold / no bold
  • # \e[4m \e[24m - underline / no underline
  • # \e[7m \e[27m - invert / no invert

Linux bash

In Linux I have the following script. What's to look out for? To display the current git branch (if any), the required command is $(__git_ps1 "%s"). It's possible to put some other characters around the %s for styling. However, this is not set initially, so first it's necessary to check and (if available) load git. There has been some refactoring so there should be one of two possible versions for the script, either git-prompt.sh or git-completion.bash. Just check for both.

if [ -f /usr/share/git/completion/git-prompt.sh ]; then
    . /usr/share/git/completion/git-prompt.sh
fi

if [ -f /usr/share/git/completion/git-completion.bash ]; then
    . /usr/share/git/completion/git-completion.bash
fi

PS1='${debian_chroot:+($debian_chroot)}\n\[\e[0;00m\]╭─[\[\e[0;36m\]\A\[\e[0;00m\]]─[\[\e[0;32m\]\u\[\e[0;00m\]]─[\[\e[0;37m\]\h\[\e[0;00m\]:\[\e[0;37m\]\W\[\e[0;33m\]$(__git_ps1 " ↪ %s")\[\e[0;00m\]]\[\e[0;92m\]\n╰\[\e[0;32m\]─>\[\e[0;00m\] '
  

This prompt configuration will look like this:

Linux bash console prompt
Linux bash console prompt

Windows with Gitbash

There's not as much to configure for Gitbash as for bash in Linux. __git_ps1 is already loaded. Unfortunately, it works a little different on Windows Gitbash. It needs to be accessed like this: `__git_ps1` and it returns something like this: (master), including the parenthesis.

PS1='\n\[\e[0;00m\]╭─[\[\e[0;36m\]\A\[\e[0;00m\]]─[\[\e[0;32m\]\u\[\e[0;00m\]]─[\[\e[0;37m\]\h\[\e[0;00m\]:\[\e[0;37m\]\W\[\033[33m\]`__git_ps1`\[\e[0;00m\]]\[\e[0;92m\]\n╰\[\e[0;32m\]─>\[\e[0;00m\] '