Echo
Ever feel like you’re living in an echo chamber? Well, great news! There’s a Unix command for that: echo
. What does echo
do? Well, it echoes. Yep, that’s right:
[mmalensek@mmalensek-vm ~]$ echo
[mmalensek@mmalensek-vm ~]$
Hmmm?
[mmalensek@mmalensek-vm ~]$ echo Hello World!
Hello World!
[mmalensek@mmalensek-vm ~]$
Okay, yeah. You get it. This might not seem like a useful program, but it does come in handy when you want to print something to the terminal from a script – a bunch of terminal commands used to automate something. Or, even more useful, you can print environment variables (or really any shell variable) with it:
[mmalensek@mmalensek-vm ~]$ echo $HOME
/home/mmmalensek
[mmalensek@mmalensek-vm ~]$ echo $?
0
I can use it to find out my home directory, or the exit status of the last command. Neat!
What can echo do?
If we want to know exactly what echo
is capable of, we can use the manual pages: run man echo
to find out all about it. If you run the command on your Linux VM, you’ll notice that echo supports a LOT of different command line flags: things that set options that will effect how the program runs. For example, echo -n Hello World!
does not print a newline character (\n
) at the end of the string.
The BSD version of echo
is a bit simpler:
NAME
echo – write arguments to the standard output
SYNOPSIS
echo [-n] [string ...]
DESCRIPTION
The echo utility writes any specified operands, separated by single blank (‘ ’) characters and followed
by a newline (‘\n’) character, to the standard output.
The following option is available:
-n Do not print the trailing newline character. This may also be achieved by appending ‘\c’ to the
end of the string, as is done by iBCS2 compatible systems. Note that this option as well as the
effect of ‘\c’ are implementation-defined in IEEE Std 1003.1-2001 (“POSIX.1”) as amended by Cor.
1-2002. Applications aiming for maximum portability are strongly encouraged to use printf(1) to
suppress the newline character.
Some shells may provide a builtin echo command which is similar or identical to this utility. Most
notably, the builtin echo in sh(1) does not accept the -n option. Consult the builtin(1) manual page.
EXIT STATUS
The echo utility exits 0 on success, and >0 if an error occurs.
But the main thrust of this command line utility is: it takes in command line arguments, and it prints them back out, one by one. Try using echo
with a whole bunch of spaces between the command line arguments. What does it do? Why?