One of the best thing about any UNIX like OS is you can customize it as you want, limited only by your imagination. Bored with the simple bash shell prompt ? Try something new 🙂 , give the bash shell prompt a trendy look with fontawesome, the PS1 environment variable and some other characters.
What is fontawesome ?
If you don't know what is fontawesome, it is an extremely popular iconic font set, extensively used in web designing, to reduce use of images, improve load time etc. etc. know more about it at wikipedia and official website. Here we are going to use it for printing the nifty icons.
1. Set the LANG variable and set character encoding
This step may not be necessary, but if you have any problem to print the special characters, it must be fixed. First check the LANG environment variable, it should be UTF-8 compatible, something like en_US.UTF-8 ,
if it's not UTF-8 compatible, set it,
Now set the terminal emulator default character encoding to UTF-8, which is default in most cases, depends upon the terminal emulator application (Konsole, Gnome-Terminal, Qterminal etc. ). This settings could be found at the preferences setting menu, you have to find out yours.
2. Download and install fontawesome
Now we have to download and install latest fontawesome true type font,
wget -c https://github.com/FortAwesome/Font-Awesome/blob/master/fonts/fontawesome-webfont.ttf sudo mkdir -p /usr/local/share/fonts sudo mv fontawesome-webfont.ttf /usr/local/share/fonts/ sudo fc-cache -v
Font installation is complete.
3. Convert unicode icon characters to proper UTF-8 HEX codes
You can't just put the fontawesome characters directly, you have to find out the proper UTF-8 encoded in HEX format. To do so, choose the icon you like, if your terminal supports directly copy paste, check it with hexdump -C command, look at the example bellow,
echo -n | hexdump -C
Cpoy only first six hex characters (3 bytes) , ef 80 87 in this case, and configure it to print the required icon character.
echo -e 'xefx80x87' # test it
just put the x prefix before every two hex character. Find out the rest characters in the same procedure.
- xefx80x87 for
- xefx87xba for
- xefx85xbc for
For more fontawesome icon characters, look at the fontawesome cheat sheet.
4. Export the proper PS1 environment variable
The PS1 environment variable is responsible for how your Bash prompt will look like, you could display almost anything with it. Just pick-up the logic and try it yourself.
Example PS1 variable, very simple prompt,
export PS1='[email protected]:w$ '
green colored prompt for normal user,
export PS1='[e[0;32m][email protected]:w$ [e[0m]'
Red colored prompt for root user,
export PS1='[e[0;31m][email protected]:w$ [e[0m]'
If you want to make this change persistence, put any one of above export PS1= line at the end of your .bashrc file.
- u - Username. The original prompt also has h, which prints the host name.
- w - Current absolute path. Use W for current relative path.
- $ - The prompt character (eg.
$for regular users).
- [ and ] - These tags should be placed around color codes so bash knows how to properly place the cursor.
This portion is taken from this archwiki, read it for more detailed information and colored bash prompts.
And now some advanced cool prompts,
export PS1=$'[e[37m]xefx80x87xefx87xbaxefx85xbc xefx80x97 @ :wxefx83xa7 [e[m]'
Red colored prompt for root user,
export PS1=$'[e[31m]xefx80x87 xefx87xba xefx83xa7 wxefx86x98 [e[m]'
Show kernel version and time,
export PS1=$'[e[37m]xefx80x87 xefx87xba [xefx85xbc `uname -r`] xefx80x97 @ :wxefx83$
So, that it, pretty cool prompts, yeah ? Try it yourself and keep experimenting 🙂 , If you need any further assistance, don't hesitate to leave a comment, and don't forget to share your prompt here !