Search notes:

Bash built-in: echo

echo writes its argument to stdout.

-e

-e enables the interpretation of backslash characters.
The following line prints Hello, world!
echo -e "\x48\x65\x6c\x6c\x6f\x2c\x20\x77\x6f\x72\x6c\x64\x21"
Writing colored output (using ANSI escape sequences):
echo -e "\e[1;31mTHIS TEXT IS RED\e[0m" 
See also ANSI Escape Codes and embedding hexadecimal characters in a string.

Supressing new lines

The echo command can be invoked with -n to suppress the new line at the end of the output:
#!/usr/bin/bash

echo -n 'Writing '
echo -n 'a '
echo -n 'text '
echo -n 'line '
echo -n 'word '
echo -n 'for '
echo word.
Github repository about-Bash, path: /built-in/echo/suppress-new-line
Compare with cmd.exe's hack of echo to achieve the same thing.

Colored output

ANSI escape sequences allow to color the output
#!/bin/bash

echo
echo ANSI escape sequences
echo ---------------------

NOCOL_="\e[0m"
BOLD__="\e[1m"
UNDERL="\e[4m"
REVERS="\e[4m" # Swap foreground and background colors (sometimes underline?)

BLACK_="\e[30m"
RED___="\e[31m"
GREEN_="\e[32m"
YELLOW="\e[33m"
BLUE__="\e[34m"
PINK__="\e[35m"
CYAN__="\e[36m"
WHITE_="\e[37m"

function print_colored {
  printf $1
  printf "  %-8s"  $2
  printf $BOLD__
  echo -n $2
  printf $NOCOL_
  echo
}

print_colored $BLACK_  Black
print_colored $RED___  Red
print_colored $GREEN_  Green
print_colored $YELLOW  Yellow
print_colored $BLUE__  Blue
print_colored $PINK__  Pink
print_colored $CYAN__  Cyan
print_colored $WHITE_  White
print_colored $UNDERL  Underln
print_colored $REVERS  Revers
Github repository about-Bash, path: /built-in/echo/colored-output

built-in echo vs /usr/bin/echo

echo is a built-in and an ordinary executable:
$ type -a echo
echo is a shell builtin
echo is /usr/bin/echo
echo is /bin/echo

See also

Bash builtins
highlight.pl

Index