echo is ashellcommand that writes input text tostandard output. It is available in manyoperating system andshells. It is often used in ashell script tolog status, provide feedback to the user and fordebugging. For an interactive session, output by default displays on theterminal screen, but output can bere-directed to afile orpiped to another process.[1]
Many shells implementecho as abuiltin command rather than an externalapplication as are many other commands.
Multiple, incompatible implementations ofecho exist in different shells. Some expand escape sequences by default, and some do not. Some accept options, and some do not. ThePOSIX specification[2] leaves the behavior unspecified if the first argument is-n or any argument contains 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, manyecho implementations are not compliant in the default environment. Because of these variations,echo is considered a non-portable command[3] and theprintf command (introduced in Ninth Edition Unix) is preferred instead.
The command is available the following shells or at least one shell of a listed operating system:
echo began withinMultics. After it was programmed inC byDoug McIlroy as a "finger exercise" and proved to be useful, it became part ofVersion 2 Unix.echo -n inVersion 7 replacedprompt, (which behaved likeecho but without terminating its output with a line delimiter).[17]
OnPWB/UNIX and laterUnix System III,echo started expandingC escape sequences such as\n with the notable difference that octal escape sequences were expressed as\0ooo instead of\ooo in C.[18]
Eighth Edition Unixecho only did the escape expansion when passed a-e option,[19] and that behaviour was copied by a few other implementations such as the builtinecho command ofBash orzsh and GNUecho.
OnMS-DOS, the command is available in versions 2 and later.[20]
C:\>echo Hello worldHello world
UsingANSI escape codeSGR sequences, compatible terminals can print out colored text.
Using aUNIX System III-style implementation:
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 withprintf:
BGRED=`printf'\33[41m'`NORMAL=`printf'\33[m'`printf'%s\n'"${BGRED}Text on red background${NORMAL}"
echo: write arguments to standard output – Shell and Utilities Reference,The Single UNIX Specification, Version 5 fromThe Open Groupecho: write arguments to standard output – Shell and Utilities Reference,The Single UNIX Specification, Version 5 fromThe Open Groupecho(1) – Plan 9 Programmer's Manual, Volume 1echo(1) – Inferno General commandsManual