DEV Community

Cover image for 🐧👾💅 The First Bash Prompt Customization I NEED to Do - Linux

🐧👾💅 The First Bash Prompt Customization I NEED to Do - Linux

While I'm developing, the terminal prompt is one of the most frequently used tools in my workflow, so I like to make it look cool.

Virgin Ubuntu

I'm using Mac to "run" Ubuntu through Multipass. The following command creates a new Ubuntu instance named cool-prompt and starts an interactive shell. The initial appearance is:

virgin appearance

PS1

We need to change the value of a variable called PS1 in the .bashrc file.

PS1 stands for "Prompt String 1". It specifically defines the primary prompt that appears before each command when the shell is ready to accept input.

.bashrc stands for "Bourne Again SHell Run Commands"; this file is a script that runs whenever the Bash shell is started.

Config for Default User

The default user is the one you get out of the box, which in the image is ubuntu as shown previously.

Open the .bashrc file and edit PS1. You can use vi since it comes with Ubuntu:

vi ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Navigate to the end of the file (use the 'G' shortcut in vi) and paste the following:

parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

PS1='\[\033[01;32m\]@\u\[\033[00m\] \[\033[01;34m\]\W\[\033[00m\]\[\033[01;36m\]$(parse_git_branch)\[\033[00m\]\n$ '
Enter fullscreen mode Exit fullscreen mode

This code will execute later at the end of the file and overwrite the value of PS1. If you know what you're doing and prefer, you can delete any previous manipulations of PS1.

This shell script defines a function to retrieve the current Git branch of a directory if it's a Git repository. It then formats the prompt with different colors for the current user's name, the current folder, and the Git branch (if applicable). Finally, it inserts a newline and $ to indicate the start of commands.

After editing the .bashrc file, every new shell session will use the new configuration. To apply the changes to the current shell prompt, use:

source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

After sourcing .bashrc, your prompt should look like this:

current folder

In a git repo:

git repo dir

Config for Root User

Youre all happy with your new prompt, but some task forces you to use the root user. To your surprise(especially if you're a noob like me), your prompt get uglier than ever.

ugly bash prompt

Ok! Don't worry. Open .bashrc again, go to the end of the file, paste the same code from the previous section. This ensures that every time a new root session is initialized, your prompt configuration will be applied. Remember to source .bashrc in the current prompt if you want the changes to take effect immediately.

Root ps1 config

And we're done !🥳!

There is more

There are much more configs possible in the shell prompt. Just to give a general idea the following link show some of the special characters that can be used in prompt variables https://www.gnu.org/software/bash/manual/html_node/Controlling-the-Prompt.html

Thanks for reading

Top comments (0)