-->

How to change background color of text in Linux Shell

Author : Piyush Gupta

Information as found on this page, excluding preview column:
Sequences are composed of the Escape character (often represented by ”^[” or ”<Esc>”) followed by some other characters: ”^[FCm” (where FC is one of the numbers in the bulleted list below).
In bash, the Esc code can be either of the following:
  1. \e
  2. \033 (octal)
  3. \x1B (hexadecimal)
Note 1: The "\e[0m" sequence removes all attributes (formatting and colors). It can be a good idea to add it at the end of each colored text.
Note 2: Foreground and background colours may vary, depending on the terminal's configuration and not all colours are supported.

Set/Reset

  • 0: Reset/remove all modifier, foreground and background attributes: echo -e "\e[0mNormal Text"
  • 1: Bold/Bright: echo -e "Normal \e[1mBold"
  • 2: Dim: echo -e "Normal \e[2mDim"
  • 4: Underlined: echo -e "Normal \e[4mUnderlined"
  • 5: Blink (doesn't work in most terminals except XTerm): echo -e "Normal \e[5mBlink"
  • 7: Reverse/Invert: echo -e "Normal \e[7minverted"
  • 8: Hidden (useful for sensitive info): echo -e "Normal \e[8mHidden Input"
  • 21: Reset/Remove bold/bright: echo -e "Normal \e[1mBold \e[21mNormal"
  • 22: Reset/Remove dim: echo -e "Normal \e[2mDim \e[22mNormal"
  • 24: Reset/Remove underline: echo -e "Normal \e[4mUnderlined \e[24mNormal"
  • 25: Reset/Remove blink: echo -e "Normal \e[5mBlink \e[25mNormal"
  • 27: Reset/Remove reverse/invert: echo -e "Normal \e[7minverted \e[27mNormal"
  • 28: Reset/Remove hidden: echo -e "Normal \e[8mHidden \e[28mNormal"

Foreground

  • 39: Default (usually green, white or light gray): echo -e "Default \e[39mDefault"
  • 30: Black: echo -e "Default \e[30mBlack" (best combined with a background colour: echo -e "Default \e[30;107mBlack on white")
  • 31: Red (don't use with green background)
  • 32: Green
  • 33: Yellow
  • 34: Blue
  • 35: Magenta/Purple
  • 36: Cyan
  • 37: Light Gray
  • 90: Dark Gray
  • 91: Light Red
  • 92: Light Green
  • 93: Light Yellow
  • 94: Light Blue
  • 95: Light Magenta/Pink
  • 96: Light Cyan
  • 97: White

Background

  • 49: Default background color (usually black or blue)
  • 40: Black
  • 41: Red
  • 42: Green
  • 43: Yellow
  • 44: Blue
  • 45: Magenta/Purple
  • 46: Cyan
  • 47: Light Gray (don't use with white foreground)
  • 100: Dark Gray (don't use with black foreground)
  • 101: Light Red
  • 102: Light Green (don't use with white foreground)
  • 103: Light Yellow (don't use with white foreground)
  • 104: Light Blue (don't use with light yellow foreground)
  • 105: Light Magenta/Pink (don't use with light foreground)
  • 106: Light Cyan (don't use with white foreground)
  • 107: White (don't use with light foreground)
To set both the foreground and background colours at once, use ther form echo -e "\e[S;FG;BGm". For example: echo -e "\e[1;97;41m" (bold white foreground on red background)
For 256 colour options, see the source page.

How to change output text color in Linux Shell

Author : Piyush Gupta

You can use these ANSI escape codes:
Black        0;30     Dark Gray     1;30
Red          0;31     Light Red     1;31
Green        0;32     Light Green   1;32
Brown/Orange 0;33     Yellow        1;33
Blue         0;34     Light Blue    1;34
Purple       0;35     Light Purple  1;35
Cyan         0;36     Light Cyan    1;36
Light Gray   0;37     White         1;37

Example:

# echo -e “e[0;31mText in Red Colore[0m”

The Above commands are showing to set foreground ( Text ) color of output on Linux bash shell. The output of above command will be in red color. There are many other colors available which you can use defined as below. Just replace “0;31” with other color values in above text.
Foreground ( Text ) Color’s:
   Black        0;30
   Red          0;31
   Green        0;32
   Yellow       0;33
   Blue         0;34
   Purple       0;35
   Cyan         0;36
   White        0;37

   Reset Text Color   0m
Use following codes to show text in Bold with same colors.
   Black        1;30
   Red          1;31
   Green        1;32
   Yellow       1;33
   Blue         1;34
   Purple       1;35
   Cyan         1;36
   White        1;37

How to prompt for User Input in Linux shell script

Author : Piyush Gupta

Command:

# read var

# read -s “Waiting for input: ” var

Uses:

read command is used for getting user input in a Linux shell script. -p switch with read command is used for showing some helpful text on-screen. Create a shell script named input.sh and add following content.
#!/bin/bash

read -p "Enter Your Name: "  username
echo "Welcome $username!"
Lets execute the shell script
# sh input.sh

Enter Your Name: Piyush
Welcome Piyush!

Input Password in Shell Script

If you want to take input of password in shell script. You must want to not to show any character on-screen entered by user. Use -s for silent mode. Using -s input characters are not echoed.
#!/bin/bash

read -s -p "Enter Password: " pswd
echo $pswd

How to extract .gz File in Linux

Author : Piyush Gupta
GZ files are archive files compressed with the "gzip" program, similar to zip files. These archive files contain one or more files, compressed into a smaller file size for faster download times from the Internet. Source code and other software program files for Linux are often distributed in .gz or .tar.gz format. Unzip a GZ file using the "gunzip" command or a .tar.gz file using the "tar" command in a Linux terminal.

Command:

$ gunzip archive.gz

Uses:

.gz is files are compressed with gzip in linux. To extract .gz files we use gunzip command.
First use following command to create gzip (.gz) archive of access.log file. Keep remember that below command will remove original file.
gzip access.log
Above command will create a archive file named access.log.gz in current directory.
ls -l access.log.gz
-rw-r--r-- 1 root root 37 Sep 14 04:02 access.log.gz
Now use gunzip command to extract access.log.gz file using command. This will extract the file from archive and remove .gz file automatically.
gunzip access.log.gz

How to remove all spaces from file in Notepad++?

Author : Piyush Gupta

There are two way to remove all spaces from file in Notepad.
  1. Using Regular expression 
  2. Direct Method
Using Regular expression:


To delete all spaces in the file, replace ' +' with '' (quotes only for demonstration, please remove them). You need to have the checkbox "Regular expression" checked.
To remove all spaces and tabs, replace '[ \t]+' with '' (remove quotes).
This works for big files, too, where the solution of direct method will be tiresome.
Direct Method:
No need for regular expressions. In Find (Used Ctrl + H) what add a white space character  and then replace with nothing.