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 variable ( 6.1 , 6.8 ) , to see how filename wildcards will expand without doing anything else to those files, or to check quoting ( 46.2 ) :
%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 C shell and most other newer shells have a version of echo that's built in ( 1.10 ) so it's faster.
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
echo
s 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.
The problem with this newer echo is that it's tough to echo an arbitrary string that might have a backslash in it. Chris Torek has a workaround: use a here-document ( 8.18 ) and cat ( 25.2 ) instead of echo . For example:
cat << END The answer is: $variable-whose-value-might-contain-backslashes
END
bash users are lucky: That shell's echo has a -e option that enables backslash interpretation, and a -E option that disables it.
printf
|
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.
The
wc
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
backquotes (
9.16
)
. (If you need a reminder of
wc
's output, see article
29.6
.)
printf
takes a formatting command from its first argument. It outputs the fourth argument after that (
%4$s
, the filename); the number of words from the second argument, in a field five wide with leading zeroes (
%2$05s
); and the number of lines from the first argument (
%1$s
); with a newline (
\n
) to finish the line: |
---|
$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 echo s. 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:
$echo
$
Without arguments, the C shell echo doesn't print the newline. To get a newline, you have to give an empty argument:
%echo ""
%
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
redirection (
13.1
)
of standard output doesn't accidentally capture the message. The Bourne shell
1>&2
operator (
45.21
)
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 not to write shell scripts with csh ( 47.2 ) .
-