Previous | Table of Contents | Next |
This example illustrates two important aspects of using escape sequences:
Whenever an escape sequence is used in the input string of an echo command, the string must be quoted to prevent the shell from expanding the escape sequence on the command line.
Also, whenever an escape sequence is used in the input string of an echo command, the string is a specification of how the output should look. Spaces should not be used to separate the escape sequences unless that is how the output needs to be formatted.
You can always rewrite any echo command that uses the \n escape sequence as several echo commands. For example, you can generate the same output as in the previous example using the echo command:
$ echo "Your fruit basket contains:" $ echo $FRUIT_BASKET
Another commonly used escape sequence is the \t sequence, which generates a tab in the output. Usually it is used when you need to make a small table or generate tabular output that is only a few lines long. As an example, the following echo command generates a small table of two users and their usernames:
$ echo "Name \tUser Name\nSriranga\tranga\nSrivathsa\tvathsa" Name User Name Sriranga ranga Srivathsa vathsa
As you can see, the heading User Name is not centered over its column. You can change this by adding another tab:
$ echo "Name\t\tUser Name\nSriranga\tranga\nSrivathsa\tvathsa" Name User Name Sriranga ranga Srivathsa vathsa
For generating large tables, the printf command, covered in the next section, is preferred because it provides a greater degree of control over the size of each column in the table.
Another commonly used escape sequence is the \c sequence, which is frequently used in shell scripts that need to generate user prompts or diagnostic output.
As you have seen in the previous example, the default behavior of echo is to add a newline at the end of its output. When you are generating a prompt, this is not the most user-friendly behavior. When the \c escape sequence is used, echo does not output a newline when it finishes printing its input string.
As an example of its use, this excerpt from a shell script
echo "Making directories, please wait...\t\c" for i ${DIRS_TO_MAKE} ; do mkdir -p $I ; done echo "Done."
produces diagnostic output that looks like the following:
Making directories, please wait... Done.
Without the \c escape at the end of the first echo statement, the output looks like the following:
Making directories, please wait... Done.
As you can see, the previous version of the output is more user-friendly, especially if you have several such lines in sequence.
Another possible use is shown in the following example:
echo "Copying files, please wait\t\c" for i in ${FILES} ; do cp $i $DEST && echo ".\c" ; done echo "\tDone."
The output is similar to the following:
Copying files, please wait ........ Done
Here a single . is printed for each file that is copied.
Note:
Some newer versions of sh, ksh, and bash include a version of echo as a built-in command that does not understand the \c escape sequence. On these shells, you might see output like the following:$ echo "Please enter your name \c" echo Please enter your name \c $Because these versions of echo print a newline after printing the \c escape sequence literally, they defeat the purpose of using this escape sequence. If you are using such a version of echo, you can switch to using either /bin/echo or /usr/bin/echo, which handle the \c escape sequence correctly.
printf
The printf command is similar to the echo command, in that it enables you to print messages to STDOUT. In its most basic form, its usage is identical to echo. For example, the following echo command:
$ echo "Is that a mango?"
is identical to the printf command:
$ printf "Is that a mango?\n"
The only major difference is that the string specified to printf explicitly requires the \n escape sequence at the end of a string, in order for a newline to print. The echo command prints the newline automatically.
Note:
The printf command is located in the directory /usr/bin on Linux, Solaris, and HP-UX machines. In addition, the printf command is a built-in command in bash.
The power of printf comes from its capability to perform complicated formatting by using format specifications. The basic syntax for this is
printf format arguments
Here, format is a string that contains one or more of the formatting sequences, and arguments are strings that correspond to the formatting sequences specified in format. For those who are familiar with the C language printf function, the formatting sequences supported by the printf command are identical.
The formatting sequences have the form:
%[-]m.nx
Here % starts the formatting sequence and x identifies the formatting sequences type. Table 13.2 gives possible values of x.
Letter | Description |
---|---|
s | String |
c | Character |
d | Decimal (integer) number |
x | Hexadecimal number |
o | Octal number |
e | Exponential floating-point number |
f | Fixed floating-point number |
g | Compact floating-point number |
Depending on the value of x, the integers m and n are interpreted differently. Usually m is the minimum length of a field, and n is the maximum length of a field. If you specify a real number format, n is treated as the precision that should be used.
The hyphen (-) left justifies a field. By default, all fields are right justified.
Previous | Table of Contents | Next |