echo (command)
In computing, echo
is a command in DOS, OS/2, Microsoft Windows, Singularity, Unix and Unix-like operating systems that outputs the strings it is being passed as arguments. It is a command typically used in shell scripts and batch files to output status text to the screen or a file.
Many shells, including all Bourne-like (such as Bash[1] or zsh,[2]) and Csh-like shells implement echo
as a builtin command.
History
echo began within Multics, and became part of Version 2 Unix. echo -n in Version 7 replaced prompt, (which behaved like echo
but without terminating its output with a line delimiter).[3]
On PWB/UNIX and later Unix System III, echo
started expanding C escape sequences such as \n
with the notable difference that octal escape sequences were expressed as \0ooo
instead of \ooo
in C.[4]
Eighth Edition Unix echo
only did the escape expansion when passed a -e
option,[5] and that behaviour was copied by a few other implementations such as the builtin echo
command of Bash or zsh and GNU echo
.
Nowadays, several incompatible implementations of echo
exist on different operating systems (often several on the same system), some of them expanding escape sequences by default, some of them not, some of them accepting options (the list of which varying with implementations), some of them not.
The POSIX Specification of echo
[6] leaves the behaviour unspecified if the first argument is -n
or any argument contain backslash characters while the Unix specification (XSI option in POSIX) mandates the expansion of (some) sequences and does not allow any option processing. In practice, many echo
implementations are not compliant in the default environment.
Because of these variations in behaviour, echo
is considered a non-portable command on Unix-like systems[7] and the printf
command (where available, introduced by Ninth Edition Unix) is preferred instead.
Usage example
> echo Hello world
Hello world
Using ANSI escape code SGR sequences, compatible terminals can print out colored text.
Using a System III-style implementation of echo
):
BGRED=`echo "\033[41m"`
FGBLUE=`echo "\033[35m"`
BGGREEN=`echo "\033[42m"`
NORMAL=`echo "\033[m"`
Or a Unix Version 8-style implementation (such as Bash when not in Unix-conformance mode):
BGRED=`echo -e "\033[41m"`
FGBLUE=`echo -e "\033[35m"`
BGGREEN=`echo -e "\033[42m"`
NORMAL=`echo -e "\033[m"`
and after:
echo "${FGBLUE} Text in blue ${NORMAL}"
echo "Text normal"
echo "${BGRED} Background in red"
echo "${BGGREEN} Background in Green and back to Normal ${NORMAL}"
Portably with printf
:
BGRED=`printf '\33[41m'`
NORMAL=`printf '\33[m'`
printf '%s\n' "${BGRED}Text on red background${NORMAL}"
See also
References
- ↑ https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html
- ↑ http://zsh.sourceforge.net/Doc/Release/Shell-Builtin-Commands.html
- ↑ McIlroy, M. D. (1987). A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971–1986 (PDF) (Technical report). CSTR. Bell Labs. 139.
- ↑ Mascheck, Sven. "echo and printf behaviour". Retrieved 24 July 2016.
- ↑ "8th Edition Unix echo man page". Retrieved 24 July 2016.
- ↑ : write arguments to standard output – Commands & Utilities Reference, The Single UNIX® Specification, Issue 7 from The Open Group
- ↑ "Autoconf documentation on echo portability". Free Software Fundation. Retrieved 24 July 2016.
External links
- : write arguments to standard output – Commands & Utilities Reference, The Single UNIX® Specification, Issue 7 from The Open Group
- Microsoft TechNet Echo article