🐧👾💅 The First Bash Prompt Customization I NEED to Do - Linux
Tags: linux, ubuntu, cli, dx
Published: 2024-07-11 03:25:49

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hkdq317g1ch7ek3tzj7z.png)

## 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:
```bash
vi ~/.bashrc
```

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

```bash
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$ '
```

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:

```bash
source ~/.bashrc
```

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

![current folder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2942e6513rq3vcm55c3o.png)

In a git repo:  
![git repo dir](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u56rvbkl10a6rtg3xgdy.png)


## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7fmg6r5rbmn3m0ytk13t.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6r4g9d4mjvfuho2aptjv.png)

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