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

15. Printing text

This section describes commands that display text strings.

15.1 echo: Print a line of text  Print a line of text.
15.2 printf: Format and print data  Format and print data.
15.3 yes: Print a string until interrupted  Print a string until interrupted.


[ < ] [ > ]   [ << ] [ 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]...

The program accepts the following options. Also see 2. Common options.

`-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'
suppress trailing newline
`\f'
form feed
`\n'
new line
`\r'
carriage return
`\t'
horizontal tab
`\v'
vertical tab
`\\'
backslash
`\nnn'
the character whose ASCII code is nnn (octal); if nnn is not a valid octal number, it is printed literally.


[ < ] [ > ]   [ << ] [ 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 in the same way as the C printf function. The format argument is re-used as necessary to convert all of the given arguments.

printf has one additional directive, `%b', which prints its argument string with `\' escapes interpreted in the same way as in the format string.

printf interprets `\0ooo' in format as an octal number (if ooo is 0 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 characters, specified as 4 hex digits hhhh, and `\U' for 32-bit Unicode characters, specified as 8 hex digits hhhhhhhh. printf outputs the Unicode characters according to the LC_CTYPE part of the current locale, i.e. depending on the values of the environment variables LC_ALL, LC_CTYPE, LANG.

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 the use of `\u' and `\U' will give an error message.

An additional escape, `\c', causes printf to produce no further output.

The only options are a lone `--help' or `--version'. See section 2. Common options.

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

 
$ /usr/local/bin/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

 
$ /usr/local/bin/printf '\u4e2d\u6587'

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

Note that in these examples, the full pathname of printf has been given, to distinguish it from the GNU bash builtin function printf.

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


[ < ] [ > ]   [ << ] [ 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.

The only options are a lone `--help' or `--version'. See section 2. Common options.


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

This document was generated by Jeff Bailey on December, 28 2002 using texi2html