How To Change Your PS1 Bash Prompt (And Add Emojis)

How To Change Your PS1 Bash Prompt (And Add Emojis)

How To Change Your PS1 Bash Prompt (And Add Emojis)

This tutorial will give you a general idea of how to get your bash prompt changed. Your Linux distro may have different file locations and/or best practices. If nothing else, you will at least learn the syntax of building a new prompt.


Bash Prompt Basics

First lets go into a bit of the bash prompt basics.

We will start with breaking apart my prompt [root@somehost ~]# and look at where it comes from.

You can see that I have used the echo command to get the contents of the $PS1 variable.

bash prompt echoing PS1 variable

The following table breaks my $PS1 variable apart into sections and what they are on the prompt.

$PS1Prompt
[[
\uroot
@@
\hsomehost
spacespace
\W~
]]
\$#
spacespace

If you put all that together, you’ve got [root@somehost ~]#. Pretty easy, right?



PS1 \ commands

Below is a table of all the different \ commands you can use in your $PS1 variable and what they will output on your prompt.

\ CommandOutput
\aan ASCII bell character (07)
\dthe date in “Weekday Month Date” format (e.g., “Tue May 26”)
\D{format}the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required
\ean ASCII escape character (033)
\hthe hostname up to the first ‘.’
\Hthe hostname
\jthe number of jobs currently managed by the shell
\lthe basename of the shell’s terminal device name
\nnewline
\rcarriage return
\sthe name of the shell, the basename of $0 (the portion following the final slash)
\tthe current time in 24-hour HH:MM:SS format
\Tthe current time in 12-hour HH:MM:SS format
\@the current time in 12-hour am/pm format
\Athe current time in 24-hour HH:MM format
\uthe username of the current user
\vthe version of bash (e.g., 2.00)
\Vthe release of bash, version + patch level (e.g., 2.00.0)
\wthe current working directory, with $HOME abbreviated with a tilde
\Wthe basename of the current working directory, with $HOME abbreviated with a tilde
\!the history number of this command
\#the command number of this command
\$if the effective UID is 0, a #, otherwise a $
\nnnthe character corresponding to the octal number nnn
\\a backslash
\[begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
\]end a sequence of non-printing characters

You can also get this info from the man page by using the man bash command.


Changing Your Bash Prompt Temporarily

Lets try changing our prompt to test what we have learned so far.

To do that, all we have to do is change the $PS1 variable.

If we wanted to add a current timestamp to the beginning of every prompt, we just need to add \t.

Simply run PS1='\t [\u@\h \W]\$ ' to change the prompt for the current session.

bash prompt changing PS1 variable temporarily

Feel free to play with all the different \ commands. If you screw something up and your terminal gets wonky, just open a new one. These changes are just temporary and only last for the current session.



Changing Your Bash Prompt Permanently

So you’ve been playing around and you found a Bash prompt that you want to stick with. How do we make it permanent?

Your $PS1 variable is read from the bashrc file. The file location depends on what Linux distribution you are using.

DistroFile
Red Hat/CentOS/Fedora/etc/bashrc
Debian/Ubuntu/etc/bash.bashrc
Suse/etc/bash.bashrc.local

I am currently using CentOS 7 so I will modify the /etc/bashrc file.

Use vi, vim, nano, your favorite editor to open the file.

At the very end of the file, add PS1='\t [\u@\h \W]\$ '(or whatever you came up with).

You can now exit the shell and open another. You should see your newly created PS1 Bash Prompt.


Advanced Bash Prompts

.bashrc is really just a shell script. That means we can add some logic to it to do different things.

What if we want the PS1 Bash prompt to look different if we are root than it would if we are a regular user?

Lets pretend for some reason we only want the current time to show up if we are the root user. I made the following bash script. Remove what you added to your bashrc file before and replace it with the following.

# UID 0 is the root user
if [[ $(id -u) -eq 0 ]];then
    # ps1 for root user
    PS1='\t [\u@\h \W]\$ '
else
    # ps1 for normal user
    PS1='[\u@\h \W]\$ '
fi

Notice that we now only get the current time when we are at the prompt as the root user.

bash prompt switching to root user

Fun with emojis!!!

We are going to do something fun(but still useful) now.

You can also add emojis to your prompt in the same way. You probably don’t have emojis on your computer keyboard though.

Head over to emojipedia.org and search for the emoji you want to add. Once you find what you’d like, just copy from the site and paste it into your terminal where you want it to go.

I am going to add the devil to my root prompt.

# UID 0 is the root user
if [[ $(id -u) -eq 0 ]];then
    # ps1 for root user
    PS1='👿 [\u@\h \W]\$ '
else
    # ps1 for normal user
    PS1='[\u@\h \W]\$ '
fi

You should now see your emoji when you are the root user!

bash prompt with devil emoji for only the root user prompt

I will also be adding a post on how to change the colors in the prompt soon. Stay tuned by subscribing to my mailing list below.

Message me on Twitter if you have any questions.