Skip to content

Instantly share code, notes, and snippets.

@ayushhdwi
Last active October 19, 2021 10:54
Show Gist options
  • Save ayushhdwi/d8deb3f2a3987783754c462285e2919d to your computer and use it in GitHub Desktop.
Save ayushhdwi/d8deb3f2a3987783754c462285e2919d to your computer and use it in GitHub Desktop.
Simple Bash Prompt Modification

Customizing Bash Prompt

First we have to open bash profile to locate bash prompt. It is available in user's home ~ directory by the name .bashrc. It is set using $PS1 variable.

In all we have $PS1, $PS2, $PS3 and $PS4 variable for the following purposes.

  • PS1 – This is the primary prompt display. This is where you set special characters or important information.
  • PS2 – This is the secondary prompt string. This is usually set as a divider between the prompt display and the text entry. It is also used to display when a long command is broken into sections with the \ sign.
  • PS3 – This is the prompt for the select command.
  • PS4 – This is the prompt for running a shell script in debug mode.

we will be changing $PS1 variable

Backup Bash Config

Before Changing You should Create a backup file using the following command.

cp ~/.bashrc ~/.bashrc.bak

To restore run the following command

cp ~/.bashrc.bak ~/.bashrc

Editing $PS1 in .bashrc

Open .bashrc file in home folder and change the $PS1 variable as shown below to see changes. Remember to Save file before closing and apply changes as using source ~/.bashrc command.

  • Set $PS1 = anytext > to show the prompt as anytext >
  • set $PS1 = \W > to show current directory in prompt as ~/Desktop/directory >
  • set $PS1 = \u > to show username in prompt as user >
  • set $PS1 = \h > to show hostname in prompt as Laptop123 >

More Bash Prompt Options

Some of these commands may not work on all versions of Linux.

  • \a – A bell character
  • \d – Date (day/month/date)
  • \D{format} – Use this to call the system to respond with the current time
  • \e – Escape character
  • \h – Hostname (short)
  • \H – Full hostname (domain name)
  • \j – Number of jobs being managed by the shell
  • \l – The basename of the shells terminal device
  • \n – New line
  • \r – Carriage return
  • \s – The name of the shell
  • \t – Time (hour:minute:second)
  • @ – Time, 12-hour AM/PM
  • \A – Time, 24-hour, without seconds
  • \u – Current username
  • \v – BASH version
  • \V – Extra information about the BASH version
  • \w – Current working directory ($HOME is represented by ~)
  • \W – The basename of the working directory ($HOME is represented by ~)
  • \! – Lists this command’s number in the history
  • \# – This command’s command number
  • \$ – Specifies whether the user is root (#) or otherwise ($)
  • \\– Backslash
  • \[ – Start a sequence of non-displayed characters (useful if you want to add a command or instruction set to the prompt)
  • \] – Close or end a sequence of non-displayed characters

Changing Color Prompt

Color Start Sequence => \[\033[00;32m\]

Color End Sequence => \[\033[00m\]

In Color Start Sequence, we have 00;32m here 00 is a code that specifies typeface and 32 specifies color.

typeface codes

• 0 – Normal • 1 – Bold (bright) • 2 – Dim • 4 – Underlined

color codes

• 30 – Black • 31 – Red • 32 – Green • 33 – Brown • 34 – Blue • 35 – Purple • 36 – Cyan • 37 – Light gray

Additionally, if you combine the bright option with a color code, you get a lighter version of that color. For example, if you use color code 1;32, you would get light green instead of the normal green. If you use 1;33, you get yellow instead of brown.

Extra: Adding padding to the gnome terminal

If your are on gnome Desktop environment, the add the following to the ~/.config/gtk-3.0/gtk.css file.

VteTerminal,
TerminalScreen,
vte-terminal {
    padding: 5px 10px 5px 10px;
    -VteTerminal-inner-border: 5px 10px 5px 10px;
}

My Configuration

Note: I use nerd fonts to display some icons that you are seeing in the prompt.

Install any nerd font from here and change your terminal font to that nerd font to see changes.

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

Screenshot

My config screenshot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment