Categories
SysOps

How to create custom bash prompt

Today, I will show you my personal shell prompt, so you could define your own custom bash prompt.

My personal shell prompt

I am using git daily, so I want to use git prompt helpers.

source /etc/bash_completion.d/git-prompt

I want to see verbose git status, so I define the following shell variables.

GIT_PS1_SHOWDIRTYSTATE=true
GIT_PS1_SHOWSTASHSTATE=true
GIT_PS1_SHOWUNTRACKEDFILES=true
GIT_PS1_SHOWUPSTREAM=verbose

My bash prompt definition.

PS1='\[\033[39m\]$(date +"%Y%m%d%H%M%S%z")\[\033[31m\] \[\033[32m\]\u\[\033[39m\]@\[\033[34m\]\h\[\033[39m\] \w \[\033[0;33m\]$(__git_ps1 "%s")\n\[\033[34m\]\$\[\033[00m\] '

It looks like this.

20200224001302+0000 milosz@development /srv/repo_blog master *% u=
$

This means that it was created on 24/02/2020 at 00:13:02 (UTC) by milosz on the development machine, inside /srv/repo_blog directory, branch master with unstaged changes, untracked files, and that there is no difference between HEAD and upstream.

Define custom bash prompt

Create ~/.bash_prompt file with custom bash prompt definition.

# ~/.bash_prompt: personal prompt
if test -f /etc/bash_completion.d/git-prompt; then
  # source git prompt
  source  /etc/bash_completion.d/git-prompt
  # use verbose mode
  GIT_PS1_SHOWDIRTYSTATE=true
  GIT_PS1_SHOWSTASHSTATE=true
  GIT_PS1_SHOWUNTRACKEDFILES=true
  GIT_PS1_SHOWUPSTREAM=verbose
  # set prompt
  PS1='[33[39m]$(date +"%Y%m%d%H%M%S%z")[33[31m] [33[32m]u[33[39m]@[33[34m]h[33[39m] w [33[0;33m]$(__git_ps1 "%s")n[33[34m]$[33[00m] '
else
  # set prompt
  PS1='[33[39m]$(date +"%Y%m%d%H%M%S%z")[33[31m] [33[32m]u[33[39m]@[33[34m]h[33[39m] wn[33[34m]$[33[00m] '
fi

Include this shell script in ~/.bashrc bash personal initialization file.

# Define prompt
[ -f ~/.bash_prompt ] && . ~/.bash_prompt

Additional notes

Inspect /usr/lib/git-core/git-sh-prompt file for more information on the git status prompt.

[...]
#
# The repository status will be displayed only if you are currently in a
# git repository. The %s token is the placeholder for the shown status.
#
# The prompt status always includes the current branch name.
#
# In addition, if you set GIT_PS1_SHOWDIRTYSTATE to a nonempty value,
# unstaged (*) and staged (+) changes will be shown next to the branch
# name.  You can configure this per-repository with the
# bash.showDirtyState variable, which defaults to true once
# GIT_PS1_SHOWDIRTYSTATE is enabled.
#
# You can also see if currently something is stashed, by setting
# GIT_PS1_SHOWSTASHSTATE to a nonempty value. If something is stashed,
# then a '

I prefer simple and consistent shell prompt, but you can use solution described here to create custom bash prompt that contains every information you need.
 will be shown next to the branch name.
#
# If you would like to see if there're untracked files, then you can set
# GIT_PS1_SHOWUNTRACKEDFILES to a nonempty value. If there're untracked
# files, then a '%' will be shown next to the branch name.  You can
# configure this per-repository with the bash.showUntrackedFiles
# variable, which defaults to true once GIT_PS1_SHOWUNTRACKEDFILES is
# enabled.
#
# If you would like to see the difference between HEAD and its upstream,
# set GIT_PS1_SHOWUPSTREAM="auto".  A "<" indicates you are behind, ">"
# indicates you are ahead, "<>" indicates you have diverged and "="
# indicates that there is no difference. You can further control
# behaviour by setting GIT_PS1_SHOWUPSTREAM to a space-separated list
# of values:
#
#     verbose       show number of commits ahead/behind (+/-) upstream
#     name          if verbose, then also show the upstream abbrev name
#     legacy        don't use the '--count' option available in recent
#                   versions of git-rev-list
#     git           always compare HEAD to @{upstream}
#     svn           always compare HEAD to your SVN upstream
#
[...]

I prefer a consistent and straightforward shell prompt, but you can use the solution described here to create a custom bash prompt that contains every information you need.