The echo command is a built-in shell command, which is used to display the value of a variable or print a line of text. Echo command plays an important role in building a shell script.

Synatx

# echo [Options] [String]

The items in square brackets are optional. A string can be defined as a finite sequence of characters (like letters, numerals, symbols punctuation marks).

When echo command is used without any options or strings, echo returns a blank line on the display screen followed by the command prompt on the subsequent line. This is because pressing the ENTER key is a signal to the system to start a new line, and thus echo repeats this signal.

Options

  • -n do not output the trailing newline
  • -e enable interpretation of backslash escapes
  • -E disable interpretation of backslash escapes (default)

If -e is in effect, the following sequences are recognized:

  • \ backslash
  • a alert (BEL)
  • b backspace
  • c produce no further output
  • e escape
  • f form feed
  • n new line
  • r carriage return
  • t horizontal tab
  • v vertical tab
  • NNN byte with octal value NNN (1 to 3 digits)
  • xHH byte with hexadecimal value HH (1 to 2 digits)

Example 1: Display the value of system defined variable

Using the set command, we can list the system define variables and to print the vaule of these variables we can use echo command:

$ echo $USER
jack
[email protected]:~$ echo $HOME
/home/jack

Example 2: Display the value of user defined variables

$ var1=`date`
$ echo "Today's date  time is : $var1"
Today's date  time is : Mon Jul 28 13:11:37 IST 2014

Example 3: Display the text string

$ echo " Hi this echo command testing"
Hi this echo command testing

Example 4: Use of backspace in echo command

$ echo -e "Ubuntu bis bthe bbest bDesktop bOS"

Above Command will Print :

Example 5: Use of tab space in echo command

$ echo -e "Ubuntu tis tthe tbest tDesktop tOS"

Above command will show below output :

Ubuntu          is         the      best     Desktop         OS

Example 6: Use of verticle tab in echo command

$ echo -e "Ubuntu vis vthe vbest vDesktop vOS"
Ubuntu 
       is 
              the 
                     best 
                            Desktop 
                                          OS

Example 7: Colored output of echo command

echo command can change the font style, background color of fonts, and font colors. Escape sequence 33 can be used to alter font properties. -e option has to be used in order for the escape sequence to be in effect. Some of the escape codes are listed below:

[0m: Normal
[1m: Bold fonts
[2m: Font color changes to Purple
[4m: Underlined fonts
[7m: Invert foreground and background colors
[8m: Invisible fonts
[9m: Cross lined fonts
[30m: Font color changes to Grey
[31m: Font color changes to Red
[32m: Font color changes to Green
[33m: Font color changes to Brown
[34m: Font color changes to Blue
[35m: Font color changes to Violet
[36m: Font color changes to Sky Blue
[37m: Font color changes to Light Grey
[38m: Font color changes to Black
[40m: Background color changes to Black
[41m: Background color changes to Red
[42m: Background color changes to Green
[43m: Background color changes to Brown
[44m: Background color changes to Blue
[45m: Background color changes to Violet
[46m: Background color changes to Sky Blue
[47m: Background color changes to Light Grey

Below command will print the output in red color.

$ echo -e "33[31mMagic of Linux33[0m"
Magic of Linux

Below Command will print “Magic of Linux” in a bold style and red background color.

$ echo -e "33[1m33[41mMagic of Linux33[0m"
Magic of Linux