How to Change Linux Prompt?

Are you bored with your Linux prompt? Do you need to customize and re-polish your prompt? Then follow us in this tutorial and we will show you how to change your default Oracle Linux prompt. Also, you will be excited to play with colors across your prompt.

Even there are a large number of tools and plugins available that can get this job done quickly for you. However, if you are a beginner to Linux it will be much more fun to do such easy customizations by yourself and this will help you get used to Linux and its commands. So, get ready and let’s get started.

Change linux prompt

Open your terminal.

By default, almost all the Linux distributions will look like the below screenshot

To view the current prompt variable, you can use the next command

echo $PS1

Note that:

  • PS1 Parameter contains the primary prompt string.
  • PS2 Parameter contains the secondary prompt string.

The output of the previous command will be:

This means:

  • \u → Current username
  • \h → Machine hostname
  • \W → Base of the current working directory
  • \$ → This indicates you are an ordinary user, not root. In case you were logged in as root instead of $ it will be #

Note:

  • PS1 → Parameter contains the primary prompt string
  • PS2 → Parameter contains the secondary prompt string

Now let’s assume you need to change your current prompt text. In the terminal write the below commands carefully and test them:

  • Display the current time in 12 Hour format
PS1='[\u \W]\$'

PS1='Hello \u Time is \@ \$'
  • Display the current time in seconds.
PS1='Hi \u Time in Seconds \T '
  • Display the current Date.
PS1='Hi \u Date is \d\\$ '
  • Display the current date with changing the colors of the prompt.
PS1='Hi \[\e[35;40m\]\u\[\e[m\] Date is \[\e[35m\]\d\[\e[m\]\\$ '
  • Display the current directory with changing the background color.
PS1='Current Dir is \[\e[42m\]\w\[\e[m\]\\$ '

In fact, the shell allows the prompt text to be customized by inserting a group of backslash-escaped special characters like the following:

  • \d Displays date in “Weekday Month Date” format.
  • \h Displays hostname.
  • \t Displays current time in 24-Hour format.

For a complete list of those special characters, you can visit the bash man page.

The previous will change the prompt temporary. In other words, once you will close the terminal everything will return back to defaults.

So, to make your changes permeant even after rebooting your system, you will need to

  • Edit the bash profile file.
nano ~/.bashrc
  • Add your PS1 value at the end of the file.
  • Save your changes by pressing CTRL + O.
  • Close the file by pressing CTRL + X.

Another little hack is coloring your terminal background. You can do this by following the next few steps:

  1. From your terminal window menu bar, choose Edit Preferences.

2. Open Color tab.

3. Uncheck “Use colors from system theme” Change the default colors for the text and background then close.

4. Your terminal background should now be changed like the below screenshot.

Congratulations you have changed your Linux prompt. You can now customize it based on your needs. These customizations can help you in your production environment, you can change the colors and text prompt of your production server and make it different from other test servers. This will let you easily distinguish between the production and test servers.

Was this article helpful?

Related Articles

Leave a Comment