Changing Linux CLI Prompt

Overview

By default, your Linux CLI prompt will look something like this:

When changing directories, I often find myself executing the pwd command to check my current path, as shown:

So a trick I like to do is change prompt format to show me the path that I am currently in. I have listed a few examples below.

Prompt Codes

Option Description
\u Username
@ The @ symbol
\h Hostname
\H Full hostname
\w Absolute full path
\W Relative path
\d Date format: Mon Jun 24
\t Time 24-hr format: 19:58:33
\T Time 12-hr format: 07:58:47
\A Time 24-hr format: 20:00
\n New line
\$ Specifies if operating as root # or user $

Prompt Styling Options

Style 1

PS1='[\u@\h: \w]\$ '

Style 2

PS1='$PWD$ '

Style 3

PS1="\[\`if [[ \$? = "0" ]]; then echo '\e[32m\h\e[0m'; else echo '\e[31m\h\e[0m' ; fi\`:\w\n\$ "

Style 4

PS1='\[`[ $? = 0 ] && X=2 || X=1; tput setaf $X`\]\h\[`tput sgr0`\]:$PWD\n\$ '

Style 5

PS1="\u@\H "

Date and Time

Use the following options to display different formats for date and time:

  • d – Displays today’s date in [weekday]/[month]/[day]
PS1="\u@\H>\d "
  • t – Displays the current time in 24-hour notation
PS1="\u@\H>\t "
  • T  Displays the current time in 12-hour notation
PS1="\u@\H>\T "
  • A – Displays the current time in 24-hour notation, with just hours and minutes
PS1="\u@\H>\A "

For further reading, checkout: https://phoenixnap.com/kb/change-bash-prompt-linux

Save Changes

To permanently save changes for the current user, the configuration will need to be added to the ~/.bashrc file.

Method 1: Echo Command

You can simply paste this in your terminal and it will append the output to the end of your file.

> echo "PS1='[\u@\h: \w]\$ '" >> ~/.bashrc

Echo the command and append to bashrc via redirect

Method 2: Edit File

Open the file using vim ~/.bashrc and scroll all the way to the bottom of the file and insert the configuration at the end of the file. Save, and exit, and now your changes are saved.

Screenshot of all files in home directory, including hidden files.
Example of the bashrc file
About the author

Arapi Tech

Networking + DevOps + Programming

Arapi Tech Blog

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Arapi Tech Blog.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.