Echo (command)
| Company type | Command |
|---|---|
| Industry | Computing / Software |
| Founded | Multics operating system (initial development) |
| Headquarters | N/A |
| Products | Shell command for text output |
echo is a shell command designed to display the provided input text to standard output. It is extensively supported across various operating systems and shells, frequently utilized within shell scripts for purposes such as status reporting, user notifications, and debugging. By default, the output is shown on the terminal display but can be redirected into a file or piped to another command.
In many cases, echo is implemented as a builtin command instead of an external executable.
There are numerous incompatible versions of echo across different shells. Some versions automatically interpret escape sequences, while others do not. Certain implementations accept options, whereas others ignore them. The POSIX standard does not define behavior when the initial argument is -n or contains backslashes; meanwhile, the Unix XSI specification requires specific expansions and prohibits option processing. Because of these variations, echo is often regarded as non-portable, leading many to prefer the printf command for consistent behavior.
Implementations
The echo command is available in shells on these platforms:
- Unix and Unix-like
- Windows
- Multics
- OS/2
- DOS
- TSC FLEX
- MetaComCo TRIPOS
- Zilog Z80-RIO
- OS-9
- Panos
- FlexOS
- ReactOS
- MPE/iX
- KolibriOS
- SymbOS
- EFI shell
History
The echo command was initially developed for the Multics operating system and subsequently rewritten in the programming language C by Doug McIlroy as a simple exercise. It quickly demonstrated practical utility and was integrated into Version 2 Unix. In the subsequent Version 7, the echo -n option replaced the earlier prompt command, which functioned similarly to echo but omitted the trailing newline.
Starting with PWB/UNIX and continuing into later releases like Unix System III, echo began supporting expansion of C-style escape sequences such as \n, with octal escapes formatted as \0ooo, differing from the traditional C notation \ooo. The version included in Eighth Edition Unix only expanded escape sequences when invoked with the -e flag—a convention that was later adopted by shells like Bash, zsh, and GNU echo.
On the MS-DOS platform, echo has been present since version 2.
Examples
Simple demonstration:
<syntaxhighlight lang="doscon"> C:\>echo Hello world Hello world </syntaxhighlight>
Terminals supporting ANSI SGR codes can produce colored output.
For a UNIX System III-style version:
<syntaxhighlight lang="bash"> BGRED=`echo "\033[41m"` FGBLUE=`echo "\033[35m"` BGGREEN=`echo "\033[42m"` NORMAL=`echo "\033[m"` </syntaxhighlight>
Or using Unix Version 8-style syntax (such as Bash without Unix-conformance mode enabled):
<syntaxhighlight lang="bash"> BGRED=`echo -e "\033[41m"` FGBLUE=`echo -e "\033[35m"` BGGREEN=`echo -e "\033[42m"` NORMAL=`echo -e "\033[m"` </syntaxhighlight>
Example usage:
<syntaxhighlight lang="bash"> echo "${FGBLUE} Text in blue ${NORMAL}" echo "Text normal" echo "${BGRED} Background in red" echo "${BGGREEN} Background in Green and back to Normal ${NORMAL}" </syntaxhighlight>
A portable approach using printf:
<syntaxhighlight lang="bash"> BGRED=`printf '\33[41m'` NORMAL=`printf '\33[m'` printf '%s\n' "${BGRED}Text on red background${NORMAL}" </syntaxhighlight>
See also
- Compilation of DOS commands
- Compilation of POSIX commands
Further reading
- Van Wolverton, MS-DOS Commands: Microsoft Quick Reference, 4th Edition, 1990, Microsoft Press, ISBN 978-1556152894
- Kathy Ivens & Brian Proffit, OS/2 Inside & Out, 1993, Osborne McGraw-Hill, ISBN 978-0078818714
- Æleen Frisch, Windows 2000 Commands Pocket Reference, 2001, O'Reilly, ISBN 978-0-596-00148-3