[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

15. Printing text

This section describes commands that display text strings.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

15.1 echo: Print a line of text

echo writes each given string to standard output, with a space between each and a newline after the last one. Synopsis:

 
echo [option]… [string]…

Due to shell aliases and built-in echo command, using an unadorned echo interactively or in a script may get you different functionality than that described here. Invoke it via env (i.e., env echo …) to avoid interference from the shell.

The program accepts the following options. Also see Common options. Options must precede operands, and the normally-special argument `--' has no special meaning and is treated like any other string.

`-n'

Do not output the trailing newline.

`-e'

Enable interpretation of the following backslash-escaped characters in each string:

`\a'

alert (bell)

`\b'

backspace

`\c'

produce no further output

`\e'

escape

`\f'

form feed

`\n'

newline

`\r'

carriage return

`\t'

horizontal tab

`\v'

vertical tab

`\\'

backslash

`\0nnn'

the eight-bit value that is the octal number nnn (zero to three octal digits)

`\nnn'

the eight-bit value that is the octal number nnn (one to three octal digits)

`\xhh'

the eight-bit value that is the hexadecimal number hh (one or two hexadecimal digits)

`-E'

Disable interpretation of backslash escapes in each string. This is the default. If `-e' and `-E' are both specified, the last one given takes effect.

If the POSIXLY_CORRECT environment variable is set, then when echo's first argument is not `-n' it outputs option-like arguments instead of treating them as options. For example, echo -ne hello outputs `-ne hello' instead of plain `hello'.

POSIX does not require support for any options, and says that the behavior of echo is implementation-defined if any string contains a backslash or if the first argument is `-n'. Portable programs can use the printf command if they need to omit trailing newlines or output control characters or backslashes. See section printf: Format and print data.

An exit status of zero indicates success, and a nonzero value indicates failure.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

15.2 printf: Format and print data

printf does formatted printing of text. Synopsis:

 
printf format [argument]…

printf prints the format string, interpreting `%' directives and `\' escapes to format numeric and string arguments in a way that is mostly similar to the C `printf' function. See (libc)Output Conversion Syntax section `printf format directives' in The GNU C Library Reference Manual, for details. The differences are listed below.

Due to shell aliases and built-in printf command, using an unadorned printf interactively or in a script may get you different functionality than that described here. Invoke it via env (i.e., env printf …) to avoid interference from the shell.

A floating-point argument must use a period before any fractional digits, but is printed according to the LC_NUMERIC category of the current locale. For example, in a locale whose radix character is a comma, the command `printf %g 3.14' outputs `3,14' whereas the command `printf %g 3,14' is an error.

printf interprets `\ooo' in format as an octal number (if ooo is 1 to 3 octal digits) specifying a character to print, and `\xhh' as a hexadecimal number (if hh is 1 to 2 hex digits) specifying a character to print.

printf interprets two character syntaxes introduced in ISO C 99: `\u' for 16-bit Unicode (ISO/IEC 10646) characters, specified as four hexadecimal digits hhhh, and `\U' for 32-bit Unicode characters, specified as eight hexadecimal digits hhhhhhhh. printf outputs the Unicode characters according to the LC_CTYPE locale. Unicode characters in the ranges U+0000...U+009F, U+D800...U+DFFF cannot be specified by this syntax, except for U+0024 ($), U+0040 (@), and U+0060 ()`.

The processing of `\u' and `\U' requires a full-featured iconv facility. It is activated on systems with glibc 2.2 (or newer), or when libiconv is installed prior to this package. Otherwise `\u' and `\U' will print as-is.

The only options are a lone `--help' or `--version'. See section Common options. Options must precede operands.

The Unicode character syntaxes are useful for writing strings in a locale independent way. For example, a string containing the Euro currency symbol

 
$ env printf '\u20AC 14.95'

will be output correctly in all locales supporting the Euro symbol (ISO-8859-15, UTF-8, and others). Similarly, a Chinese string

 
$ env printf '\u4e2d\u6587'

will be output correctly in all Chinese locales (GB2312, BIG5, UTF-8, etc).

Note that in these examples, the printf command has been invoked via env to ensure that we run the program found via your shell's search path, and not a shell alias or a built-in function.

For larger strings, you don't need to look up the hexadecimal code values of each character one by one. ASCII characters mixed with \u escape sequences is also known as the JAVA source file encoding. You can use GNU recode 3.5c (or newer) to convert strings to this encoding. Here is how to convert a piece of text into a shell script which will output this text in a locale-independent way:

 
$ LC_CTYPE=zh_CN.big5 /usr/local/bin/printf \
    '\u4e2d\u6587\n' > sample.txt
$ recode BIG5..JAVA < sample.txt \
    | sed -e "s|^|/usr/local/bin/printf '|" -e "s|$|\\\\n'|" \
    > sample.sh

An exit status of zero indicates success, and a nonzero value indicates failure.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

15.3 yes: Print a string until interrupted

yes prints the command line arguments, separated by spaces and followed by a newline, forever until it is killed. If no arguments are given, it prints `y' followed by a newline forever until killed.

Upon a write error, yes exits with status `1'.

The only options are a lone `--help' or `--version'. To output an argument that begins with `-', precede it with `--', e.g., `yes -- --help'. See section Common options.


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated on January, 20 2010 using texi2html 1.76.