Shorten bash shell prompt (and add colors!)
Rei Allen Ramos

Rei Allen Ramos @reiallenramos

About: Ruby/Ember dev learning to blog. Writing simple and straight-to-the-point tutorials!

Location:
Singapore
Joined:
Apr 19, 2020

Shorten bash shell prompt (and add colors!)

Publish Date: Apr 20 '20
7 1

In a previous article, we learned how to append the current git branch in your bash shell:

But if you want to turn your bash shell prompt from this:
Alt Text
to this:
Alt Text
then keep reading 🦄

Shorten bash shell prompt

Fire up your editor and open ~/.bashrc. Look for the block we edited from the previous article:

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(show_git_branch)\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(show_git_branch)\$ '
fi
unset color_prompt force_color_prompt

If you want to learn about each and every piece of the script, feel free to head on to the reference section below. The article even talks about how to customize the colors. Otherwise, copy-paste away!

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]\[\033[01;33m\]$(show_git_branch)\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@:\W$(show_git_branch)\$ '
fi
unset color_prompt force_color_prompt

As you might have noticed by now, this also changes the color of the git branch part. Enjoy your short and sweet prompt!

Reference:
BASH Shell Change The Color of Shell Prompt on Linux or UNIX

Comments 1 total

  • Guido Barosio
    Guido BarosioApr 21, 2020

    (loved the pug)
    If for some reason you paste this directly in your terminal, simply type reset to get things back in place.

Add comment