Insights from Our Experts
Customizing the Unix/Linux terminal
If you are using Terminal regularly at your work you might be bored at how it looks, you might find it appropriate to change the way it looks beyond the background and transparency settings.
Customizing the Terminal appearance can be quite easy and fun.
Before we go to customization, we need to know, the command prompt is controlled by several shell variables like PS1, PS2, PS3, and PS4. For the sake of simplicity, we care about PS1 only. To know the current PS1, just echo the variable $PS1.We will get a string with several backslash-escaped special characters. Some of them are
- \u – current username
- \h – hostname of machine
- \w – working directory
- \$ – prompt character: # if it is root, $ if it isn’t.
Equates to a prompt of ubuntu@ubuntu:~$, where the first ubuntu is username and the second ubuntu is hostname. There are a number of backslash-escaped special characters like this and by using them we edit the PS1 variable.
For example, to display the host name, date and current directory the PS1 should be
PS1=” [\d \t \u@\h \w] $ ”
Display it using the ‘export’ command.
There are a lot many special characters like this to manipulate the command prompt.
To add colors to the shell prompt we need to use the following command syntax
‘ \e[x;ym $PS1\e[m ‘
The meaning of the syntax is
\e[ – Start color scheme
x;y – color pair to use (x,y)
$PS1 – is the shell prompt
\e[m – end color scheme
And there is a list of color codes such as \e[0;31m for Red, \e[0;32m for Green \e[0;34m for Blue etc... which can be used.
For example to colorize the Hello World prompt above we use the following command:
PS1 = “\[e[1;31m\]hello\[e[1;34m] world:”
The result will be hello world in the command prompt in Red and Blue colors.
Now go on and edit your terminal and make it as awesome as you can!