The echo command writes its command-line arguments and a newline to the standard output. Shell scripts use echo for sending text to the terminal, down a pipe, and into a file. You can use echo on the command line to show the value of a , to see how filename wildcards will expand without doing anything else to those files, or :
echo "USER is $USER."USER is jerry. %
echo "All 'a' files are: " a*All 'a' files are: abacus apple axes
The printf command gives you more formatting control.
The original echo, and the csh echo which acts like it, have just one option. The -n option tells echo not to print a newline after the message. Shell scripts use -n to send a question to a user and leave the cursor at the end of the message:
echo -n "Enter your name: "
(The space at the end makes the prompt look better. The quotes make the shell pass that space on to echo.)
Newer versions of echo check their arguments for a backslash (
This marks the start of an escape sequence, a character
that the backslash and the next letter stand for.
For example, when these newer echos see
\n, they print
a newline character:
echo "1.\n2.\n3."1. 2. 3. $
In this version of echo, a
\c at the end of the last argument suppresses
the newline - like the -n option does in the other echo:
echo "Enter your name: \c"
Your online echo (or csh) manual page should tell you which version you have and list any escape sequences.
cat << END The answer is: $
bash users are lucky: That shell's echo has a -e option that enables backslash interpretation, and a -E option that disables it.
Another utility called printf works like the printf(3)
routine in the C language; it handles escape sequences, lets you set
field widths, and more.
(The GNU version is on the CD-ROM.)
Here's an example.
command gives the number of lines, words, and characters in a file,
followed by the filename.
We pass those four fields in the wc output to the printf
command line with
(If you need a reminder of wc's output, see article
printf takes a formatting command from its first argument.
It outputs the fourth argument after that (|
printf 'The %4$s file has %2$05s words on %1$s lines.\n' `wc count`The count file has 00235 words on 42 lines.
Because printf isn't built into any shells I know of, it's more portable than the crazy set of echos. If we had printf and the old echo, life would be easier. Article 46.10 shows a way to make echo portable.
The C shell echo works differently from other versions. For example, to make an empty line with the standard echo, don't give any arguments. (This is usually done for readability - to put blank lines between other output.) Standard echo will just print a newline:
Without arguments, the C shell echo doesn't print the newline. To get a newline, you have to give an empty argument:
To use the standard echo from the C shell, type /bin/echo instead.
echo writes to standard output. Error messages in shell scripts should be written to the standard error so that of standard output doesn't accidentally capture the message. The Bourne shell will move echo's output to standard error:
echo "progname: choke wheeze complain" 1>&2
The C shell can't do that - which is another reason.