2.10. Output

Output commands are only occasionally useful at the interactive command line. We may sometimes use them to take a quick look at a variable such as $PATH.

shell-prompt: echo $PATH
	

Output commands are far more useful in scripts, and are used in the same ways as output statements in any programming language.

The echo command is commonly used to output something to the terminal screen:

shell-prompt: echo 'Hello!'
Hello!
shell-prompt: echo $PATH
/usr/local/bin:/home/bacon/scripts:/home/bacon/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin
	

However, echo should be avoided, since it is not portable across different shells and even the same shell on different Unix systems. There are many different implementations of echo commands, some internal to the shell and some external programs. Different implementations of echo use different command-line flags and special characters to control output formatting.

In addition, the output formatting capabilities of echo commands are extremely limited.

The printf command supersedes echo. It has a rich set of capabilities and is specified in the POSIX.2 standard, so its behavior is the same on all Unix systems.

The printf command is an external command, so it is independent of which shell you are using.

The functionality of printf closely matches that of the printf() function in the standard C library. It recognizes special characters such as '\n' (line feed), '\t' (tab), '\r' (carriage return), etc. and can print numbers in different bases.

shell-prompt: printf 'Hello!\n'
Hello!
	

The basic syntax of a printf command is as follows:

printf format-string argument [argument ...]
	

The format-string contains literal text and a format specifier to match each of the arguments that follows.

Each format specifier begins with a '%' and is followed by a symbol indicating the format in which to print the argument.

Table 2.3. Printf Format Specifiers

SpecifierOutput
%sString
%dDecimal number
%oOctal number

The printf command also recognized most of the same special character sequences as the C printf() function:

Table 2.4. Special Character Sequences

SequenceMeaning
\nNewline (move down to next line)
\rCarriage Return (go to beginning of current line)
\tTab (go to next tab stop)

printf "%s %d %o\n" 10 10 10
	

Output:

10 10 12
	

There are many other format specifiers and special character sequences. For complete information, run man printf.

To direct printf output to the standard error instead of the standard output, we simply take advantage of device independence and use redirection:

	printf 'Hello!\n' >> /dev/stderr
	

Practice Break

Sync-point: Instructor: Make sure everyone in class succeeds at this exercise before moving on.

Write a shell script containing the printf statement above and run it. Write the same script using two different shells, such as Bourne shell and C shell. What is the difference between the two scripts?

2.10.1. Self-test

  1. What are the advantages of printf over the echo command?
  2. Does printf work under all shells? Why or why not?