echo (command)
In computing, echo
is a command in DOS, OS/2, Microsoft Windows, Singularity, Unix and Unix-like operating systems that places a string on the computer terminal. 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 Bash[1] and zsh,[2] implement echo
as a builtin command.
Contents
History
echo began within Multics, and became part of Version 2 Unix. echo -n in Version 7 replaced prompt, which behaved like the command without a newline.[3]
Usage example
> echo Hello world Hello world
Using ANSI escape code SGR sequences, compatible terminals can print out colored text:
FGRED=`echo "\033[31m"`
FGCYAN=`echo "\033[36m"`
BGRED=`echo "\033[41m"`
FGBLUE=`echo "\033[35m"`
BGGREEN=`echo "\033[42m"`
NORMAL=`echo "\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}"
Some variants of Unix, such as Linux, support the options -n
and -e
, and do not process escape sequences unless the -e
option is supplied. For example, FGRED=`echo -e "\033[31m"`
might be used under Linux. Unfortunately, such options are non standard[4] due to historical incompatibilities between BSD and System V; the printf
command can be used in situations where this is a problem. It is therefore recommended that printf
be used to ensure that escape sequences are processed. The equivalent code using printf
is simply FGRED=`printf "\033[31m"`
.
Implementation example
The echo
command can be implemented in the C programming language with only a few lines of code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int i;
if (argc > 1)
printf("%s", argv[1]);
for (i = 2; i < argc; i++)
printf(" %s", argv[i]);
printf("\n");
return EXIT_SUCCESS;
}
Scripting Languages can also emulate echo quite simply:
$ perl -e 'print " @ARGV\n"
' This is a test. This is a test. $ python -c "import sys; print ' '.join(sys.argv[1:])
" This is a test. This is a test.
See also
References
<templatestyles src="Reflist/styles.css" />
Cite error: Invalid <references>
tag; parameter "group" is allowed only.
<references />
, or <references group="..." />
External links
- The Single UNIX® Specification, Issue 7 from The Open Group : write arguments to standard output – Commands & Utilities Reference,
- Microsoft TechNet Echo article
- ↑ https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html
- ↑ http://zsh.sourceforge.net/Doc/Release/Shell-Builtin-Commands.html
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ IEEE Std 1003.1, 2004, documentation for echo