Bash - Echo (Bultin Command)

> Procedural Languages > Bash Shell and (Unix|Linux) Utilities (XCU)

1 - About

The echo builtin bash command accepts a string or a variable as standard input, and echoes it back to the terminal as standard output.

type echo
echo is a shell builtin

This is the supersede version of Bash then not /bin/echo.

You can also use Bash - printf (Builtin command)

Advertising

3 - Syntax

To echo a string:

echo [OPTION]... [STRING]...

where:

  • -n: do not output the trailing newline. See below trailing newline
  • -e: enable interpretation of backslash escapes
  • -E: disable interpretation of backslash escapes (default)
  • –help display this help and exit
  • –version: output version information and exit

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

  • \0NNN the character whose ASCII code is NNN (octal)
  • \\ backslash
  • \a alert (BEL)
  • \b backspace
  • \c suppress trailing newline
  • \f form feed
  • \n new line
  • \r carriage return
  • \t horizontal tab
  • \v vertical tab

See Text - Control Characters

Advertising

4 - Example

4.1 - Interpretation of non visible (''\'') characters

See Text - Control Characters

  • with the e option and with the double quoted string
var="hello Nico\nHello Nico"!
echo -e "$var" 
hello Nico
Hello Nico!
  • without the e options and with single quoted string
var=$'hello Nico\nHello Nico!'
echo "$var"
hello Nico
Hello Nico!

4.2 - trailing newline

  • with -n
for a in `seq 10`
do
  echo -n "$a "
done
1 2 3 4 5 6 7 8 9 10
  • without -n
for a in `seq 10`
do
  echo "$a "
done
1
2
3
4
5
6
7
8
9
10
Advertising