Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [18]

By Root 856 0
terminated with a newline:

$ echo Now is the time for all good men

Now is the time for all good men

$ echo to come to the aid of their country.

to come to the aid of their country.

* * *

echo


Usage

echo [ string ... ]

Purpose

To produce output from shell scripts.

Major options

None.

Behavior

echo prints each argument to standard output, separated by a single space and terminated by a newline. It interprets escape sequences within each string that represent special characters and also control its behavior.

Caveats

Historical differences in behavior among Unix variants make it difficult to use echo portably for all but the simplest kinds of output.

Many versions support a -n option. When supplied, echo omits the final newline from its output. This is useful for printing prompts. However, the current POSIX-standard version of echo does not include this option. See the discussion in the text.

* * *

Unfortunately, over time, different versions of echo developed. The BSD version accepted a first argument of -n, which would make it omit the trailing newline. For example (the underscore represents the terminal's cursor):

$ echo -n "Enter your name: "

Print prompt

Enter your name: _ Enter data

The System V version interpreted special escape sequences (explained shortly) within the arguments. For example, \c indicated that echo should not print the final newline:

$ echo "Enter your name: \c"

Print prompt

Enter your name: _ Enter data

Escape sequences are a way to represent hard-to-type or hard-to-see characters within a program. When echo sees an escape sequence, it prints the corresponding character. The valid escape sequences are listed in Table 2-2.

Table 2-2. echo escape sequences

Sequence

Description

\a

Alert character, usually the ASCII BEL character.

\b

Backspace.

\c

Suppress the final newline in the output. Furthermore, any characters left in the argument, and any following arguments, are ignored (not printed).

\f

Formfeed.

\n

Newline.

\r

Carriage return.

\t

Horizontal tab.

\v

Vertical tab.

\

A literal backslash character.

\0 ddd

Character represented as a 1- to 3-digit octal value.

When shell scripting, the \a sequence is most useful for getting a user's attention. The \0 ddd sequence is useful for (very) primitive cursor manipulation by sending terminal escape sequences, but we don't recommend this.

Since many systems still default to the BSD behavior for echo, we only use its simplest form throughout this book. We use printf for more complicated output.

Fancier Output with printf

The differences between the two versions of echo led to one of the most infamous of the Unix-variant portability headaches. During the first round of standardization for POSIX, the committee members could not agree on how to standardize echo, so they came up with a compromise. While echo was part of the POSIX standard, the standard didn't specify the behavior if the first argument was -n, or if any argument contained escape sequences. Instead the behavior was left as implementation-defined, meaning that each vendor was required to document what its version of echo does.[5] In effect, echo could be used portably only if it was used in the simplest fashion. Instead, they adopted the printf command from the Ninth Edition Research Unix system. This command is more flexible than echo, but at the cost of some added complexity.

The printf command is modeled after the printf( ) library routine from the C library. It closely duplicates the facilities of that function (see the manual pages for printf(3)), and it's likely that if you've done any programming in C, C++, awk, Perl, Python, or Tcl, you're familiar with the basics. Of course, there are a few quirks specific to the shell-level version.

The printf command can output a simple string just like the echo command:

printf "Hello, world\n"

The main difference that you will notice immediately is that, unlike echo, printf does not automatically

Return Main Page Previous Page Next Page

®Online Book Reader