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.
| Character | Specification | Extra info |
|---|---|---|
\a | A bell character | Based on it's name the bell character makes some noise, like a beep, mostly used to get a user's attention |
\d | Specifies a date | It's in a Weekday, Month, Day format so as of today it's Tue Jan 23 |
\D{format} | Specifies a date | Does the same as the above, only this time you can control the format of the date (more on formats below) |
\h | The hostname up to the first '.' | In case of example.com it would be example |
\H | The full hostname | In case of example.com it would be example.com |
\t | Specifies the time | It's the current time in a 24 hour format (HH:MM:SS) |
\T | Specifies the time | It's the current time in a 12 hour format (HH:MM:SS) |
\@ | Specifies the time | It's the current time in a 12 hour format (HH:MM (am/pm)) |
\A | Specifies the time | It's the current time in a 24 hour format (HH:MM) |
\u | The name of the current user | It's the name of the user using the terminal, sudo changes it to root |
\w | The current working directory | The home directory of the user is replaced with a ~ |
\$ | Character based on user | prints "#" 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:
| Character | Specification | Example |
|---|---|---|
%a | Short name of the weekday | Tue |
%A | Full name of the weekday | Tuesday |
%b | Short name of the month | Jan |
%B | Full name of the month | January |
%c | Full date and time | Tue Jan 23 16:00:30 2018 |
%e | Day of the month | 1-31 |
%m | Month specified with numbers | 1-12 |
%M | Minutes | 0-59 |
%p | AM/PM Character | PM |
%r | 12 hour time format | 04:00:30 pm |
%R | 24 hour time format | 16:00:30 |
%S | Seconds | 30 |
%Y | Year | 2018 |
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 Code | Color |
|---|---|
0m | No color |
0;30m | Black |
0;31m | Red |
0;32m | Green |
0;33m | Orange |
0;34m | Blue |
0;35m | Purple |
0;36m | Cyan |
0;37m | Light 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