INTRO

In my previous posts, I have shown you some frameworks and utilities to customize your Terminal. We learned about ZSH customization and Bash customization with the help of Oh My Bash and Oh my ZSH which makes customization easy for your Terminal. But have you ever thought how do they make these themes so beautiful? well, that’s what we will learn today.

In this article, you will learn to make not the whole thing but some basics that are very necessary for anyone related to our field. Programmers use it on their programs without being aware of it. hackers can make their script look more fancy with colours. We are going to learn ANSI Escape Characters. We will learn to edit .bashrc file.

But before going more further, I would love to suggest some of my previous posts for a better understanding of this post.

  1. ZSH – Oh My Zsh
  2. Bash – Oh My Bash

Both of these articles are very important to read once, in case you haven’t. After reading these, your basic concept will be clear and you will be able to customize your terminal as shown in these articles.

 

SETUP

Have you ever wondered, how some tools print a string with colours? for example, when you run the Metasploit you will see its colourful banners (different each time).

Screenshot:

Another example can be Wfuzz. when you specify -c flag in fuzz, you will see the output in colours.

Screenshot:

All those colour outputs depend on ANSI escape codes directly or indirectly. there are some python libraries (Colored) for colourful output which use ANSI escape codes.

ANSI escape sequences are a standard for in-band signalling to control the cursor location, colour, and other options on video text terminals and terminal emulators. Certain sequences of bytes, most starting with Esc (ASCII character 27) and ‘[‘, are embedded into the text, which the terminal looks for and interprets as commands, not as character codes. Read more….

 

Python

Let’s start with a very famous and simple language python. I will use bash in a minute. but I am choosing Python. because everyone prefers it in everything. To print a string in Red colour, Type this command:

print u"\u001b[31mLuckythandel"

Screenshot:

If you are using python3, you won’t need to specify ‘u’ before the string. ‘u’ is for telling the program that we are using Unicode string. For example, if I want to print my full name with a space between my first name and sir then it would be like this:

python3 -c "print u'Lucky\u0020Thandel'"

Screenshot:

 

try to use it without ‘u’ then the Unicode will be treated as a string.

python -c "print ('Lucky\u0020Thandel')"

Screenshot:

So, let’s get back to colour strings. we printed a red string. but what if you want to print only one word red instead of the whole line. then you will need to use reset code. For example, the string is “Game of Thrones”  and you want to print “Game” Red. so, to do that, type this command:

 python -c 'print u"\u001b[31mGame\u001b[0m of Thrones"'

Screenshot:

as you can see the \u001b[0m code has reset the value of Unicode \u001b[31m.

You can print more colourful words by changing the value <31> in Unicode. you can google about it to know more. I will leave it on you.

There is one more method without using Unicode and ‘u’. type this command:

python -c 'print "\033[91m New Way\033[00m"'

To change colours, change the value <91>. I use this method to print colourful strings in my python scripts.

 

Bash

Bash is a very popular shell in Linux distributions. almost every Linux OS use Bash as a default shell. We are going to edit the .bashrc file. so, I would recommend you to use a different user account or create a backup of your .bashrc file. I am using a different account.

To create a user, type this command:

adduser <username>

Screenshot:
Now, let’s login to the new user. type this command:

su - <username>

Screenshot:

As I told you that the bash is a default shell. any newly created user will have the bash shell as default.

Let’s print some colourful strings with echo. type this command:

echo -e "Color \e[31mGreen"

Screenshot:

-e is to tell the echo that the backslash in between the string is not to print. it will interpret with backslash escape char.

There is one cool thing I want to show you. you can blink the string with this code:

echo -e '\e[5mIT IS MAGIC\e[0m'

Always remember to use the reset code echo \e[0m.  you can use \003 or \x1B instead of \e. V

visit this link to see more what else you can do.

Now we are moving on to edit .bashrc file. let me remind you again to back up your .bashrc file.

Type this command:

nano ~/.bashrc

Screenshot:

Find the PS1 variable. the PS1 value is the value of what you see before your command. I don’t know what we call it. But you see the result value after changing it. edit the first PS1 value.

replace the PS1 value to “\e[7mCOMMAND \e[27m==>“.

Screenshot:

save it and type bash in the terminal to reload the bash.

Screenshot:

You can try some other advance combinations where instead of a string you will call a variable. for, example: you want to type the user name with an underline.

$PS1='\e[7mCOMMAND \e[27mFor\e[4m $USER\e[0m==>'

Screenshot:

I am pretty sure that you can make more beautiful codes with ordinary variables. use colour to make it look more beautiful. I would never recommend editing .bashrc  of the root.

 

 

Thanks For Visiting.

LEAVE A REPLY

Please enter your comment!
Please enter your name here