Customizing My Bash Prompt


While in the process of improving all my dotfile configurations I came across the $PS1 environment variable in my ~/.bashrc file that specifies what my default interactive prompt looks like in bash. Since I got my laptop running Pop!_OS it has been the default configuration below:

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

Which would render in the $HOME directory as: justanesta@pop-os:~$  

While this was a perfectly suitable default. I don't really understand the point of having the hostname (specified by the \h variable) in the command prompt. I also have never really loved the traditional dollar sign $ at the end of command prompts.

I have seen countless examples of people customizing their command prompts and thought it was high time I roll out one of my own. After getting inspired by this elegant and simple example from developer Lars Kappert I played around with this helpful click-and-drop visual Bash Prompt Generator as well as these helpful reference resources and came up with the following:

PS1="${CLR_USER}\u${CLR_RESET} ${CLR_PATH}\w${CLR_RESET} ${CLR_GIT}\$(__git_prompt)${CLR_RESET} > "

Where here __git_prompt was customized elsewhere in the ~/.bashrc file with the current git branch name (if applicable), otherwise nothing.

__git_branch() {
  # Prints current branch name, or nothing if not in a git repo
  git symbolic-ref --short HEAD 2>/dev/null
}

__git_prompt() {
  local b="$(__git_branch)"
  if [ -n "$b" ]; then
    printf "%s" "$b"
  fi
}

All this gave me what I really wanted: My username, the current working directory, and the current git branch name if the current working directory has an associated git repository. In my current working directory for this site it now looks like:

justanesta ~/projects/justanesta-site main >

Looking forward to customizing more options in my dotfiles and having them managed in version control so that they are portable to different machines and even operating systems!