← All commands
THE COMMAND REFERENCE

echo

Print text followed by a newline.

Last updated

macOS / Apple

Implementation-level reference; examples are not certified for every installed version.

Overview

echo prints its arguments, separated by single spaces, and ends with a newline. It is the simplest way to show a message or to put a known line into a pipe or a file. Every shell has its own echo builtin, and there is also an /bin/echo program; they agree on plain text and disagree on options and on backslashes, which is why printf is safer for anything but plain text.

Background

The macOS manual for echo says the utility conforms to POSIX, whose specification leaves option handling and backslash escapes to the implementation. Today the name covers the Bash builtin, GNU echo, the echo that macOS ships, and the echo applet in BusyBox. They agree on printing the arguments and a newline.

Examples

Print a word:

echo hello

Print several words; the arguments are joined with single spaces:

echo one two

Keep extra spaces by quoting the text:

echo "a   b"

Print the result of an arithmetic expression:

echo "$((1 + 2))"

Write a line to a file and read it back:

echo hi > out.txt && cat out.txt

Print an empty line by giving no arguments:

echo

Common mistakes

Without quotes, the shell splits and expands the text first, so echo * prints the file names and extra spaces vanish. -n (no newline) and -e (interpret backslashes) are not portable: the builtin of a given shell, GNU echo and macOS /bin/echo treat them differently, and some print -n as text. Use printf for anything with backslashes, no newline or a leading dash. echo cannot reliably print text that starts with -.

Portability

Only plain arguments, quoting and redirection are in the Common examples, which gave the same output in the Bash builtin, GNU echo, macOS /bin/echo and BusyBox echo. In a plain sh script the builtin may behave differently from both Bash and the executable; the macOS /bin/sh, for example, printed -n as text for echo -n in the check. The Common examples were executed on Bash 5.2, GNU coreutils 9.11, macOS system tools, and BusyBox 1.37.0 (see the development guide for the exact check).

About macOS echo

This is the echo in the system folders of macOS. It follows the BSD implementation, and is the executable that runs when you call it by path, as in /bin/echo. It supports -n, but -e is not recognized and is printed as text.

Apple examples

Leave out the newline with -n:

echo -n hi; echo

Options

-n
Do not print the newline at the end.

Apple notes

macOS /bin/echo has no -e: /bin/echo -e 'a\tb' prints -e a\tb as text. Use printf for escapes.

References

Original explanations, with upstream documentation for further reading. FreeBSD references are comparison material, not a claim of Apple version equivalence.