Customise Your Bash Shell!

ah101(51)
Published in
#linux
Words
893
Reading
4 min
Listen
Play
9y

About a year ago, when i first got my VPS i was using the shell, non-stop writing commands and giving arguments to applications. After a while i got bored of the same black & white style, and i saw other people having colored, customised shells. So I decided to do a bit of research and here is what you can do to customise your bash shell to your needs:


What to print?

Bash has a lot of cool built-in special characters, they're in the format of the well known \n character: a slash and a character, let's take a look at some of the special characters you can use in your shell.

CharacterSpecificationExtra info
\aA bell characterBased on it's name the bell character makes some noise, like a beep, mostly used to get a user's attention
\dSpecifies a dateIt's in a Weekday, Month, Day format so as of today it's Tue Jan 23
\D{format}Specifies a dateDoes the same as the above, only this time you can
control the format of the date (more on formats below)
\hThe hostname up to the first '.'In case of example.com it would be example
\HThe full hostnameIn case of example.com it would be example.com
\tSpecifies the timeIt's the current time in a 24 hour format (HH:MM:SS)
\TSpecifies the timeIt's the current time in a 12 hour format (HH:MM:SS)
\@Specifies the timeIt's the current time in a 12 hour format (HH:MM (am/pm))
\ASpecifies the timeIt's the current time in a 24 hour format (HH:MM)
\uThe name of the current userIt's the name of the user using the terminal, sudo changes it to root
\wThe current working directoryThe home directory of the user is replaced with a ~
\$Character based on userprints "#" if the user is root, otherwise it prints "$"

Additionally you can use strings like : or -> and you can also use the power of bash: ${HOME}


Time Formats

At \D we saw that we need a {format} parameter to display the date/time we want. It uses the strftime C++ function and passes the format specified to it. Here are some of the formats:

CharacterSpecificationExample
%aShort name of the weekdayTue
%AFull name of the weekdayTuesday
%bShort name of the monthJan
%BFull name of the monthJanuary
%cFull date and timeTue Jan 23 16:00:30 2018
%eDay of the month1-31
%mMonth specified with numbers1-12
%MMinutes0-59
%pAM/PM CharacterPM
%r12 hour time format04:00:30 pm
%R24 hour time format16:00:30
%SSeconds30
%YYear2018

Colors!

Ok, so now we know what to print on the shell, but we still have the plain old black & white style shell.
To add colors we need to learn two more special bash characters:
\[-> Beginning of non-printed characters
\]-> End of non-printed characters
The format for the colors is going to be:
\[\033[<color code>\] after this every character will be printed with the spcified color, until another color code is specified.
\033 is an octal ascii key code, it's the ESC or escape character
So here is a list of color codes:

Color CodeColor
0mNo color
0;30mBlack
0;31mRed
0;32mGreen
0;33mOrange
0;34mBlue
0;35mPurple
0;36mCyan
0;37mLight Gray

Important: Replacing the 0 with a 1 in the beginning of the color code, gives a light version of the original listed color. So for example replacing 0;31m with 1;31m gives you Light Red instead of Red
So a color code for yellow looks like: \[\033[1;33m\]


Customise your shell

Now that we know some options to play with let's try them out.
Bash stores the beginning text of the terminal in the $PS1 variable.
You can edit it by typing:
PS1="your code here"

Examples

Printing the current Time and the user character

PS1="\A \$ "
Prints: 16:30 # (I'm running as root)

Printing the user@hostname and the user character

PS1="\u@\h\$ "
Prints: root@iLinux# (I'm running as root)

Printing the hostname:working dir and the user character

hostname will be yellow and working dir will be light blue rest of the input will be light green the ":" and "$/#" will be white
\[\033[1;33m\]\h\[\033[1;37m\]:\[\033[1;34m\]\w\[\033[1;37m\]\$\[\033[1;32m\]
Prints: iLinux:/var/www# (I'm running as root) with the correct colors


What's next

Ok so now you know how and what to change, but this change is not yet persistent.

Resetting the terminal

If you somehow messed up your terminal you can reset it by executing bash, it will load a new bash shell with the default PS1 set. Or you can just open a new terminal window.

Making the changes persistent

You need to edit something called the .bashrc file. The code in this file runs, whenever a new bash shell starts.
Setting PS1 there will make sure to load it into all new bash shells.
To edit your user's bashrc open: ~/.bashrc
And add the same command we've been executing previously to the end of the file.
Note: If you add it to the end of the file, you can make sure that no other lines override your PS1 settings
Save & Close the file. To see the changes open a new terminal or type source ~/.bashrc or simply type bash


Summary

It's nice that bash does all this. I honestly felt better with my edited PS1 settings, because i like colorful things. I just wanted to add: make sure there are not too many or no information at all in your prompt. I wouldn't recommend to remove the working directory from PS1 for example.
Have fun customising your shell :)


Sources

Bash Documentation
Color Codes
Time Format Codes

P.S
If you're stuck i found an online point and click PS1 editor

Customise Your Bash Shell! | Ecency