Previous Section  < Day Day Up >  Next Section

9.19. Printing the Values of Variables in the TC Shell

9.19.1 The echo Command

The built-in echo command prints its arguments to standard output. The echo allows the use of numerous escape sequences that are interpreted and displayed as tabs, newlines, form feed, and so on. Table 9.21 lists the echo options and escape sequences.

Table 9.21. echo Options and Escape Sequences

Option

Meaning

-n

Suppresses newline at the end of a line of output

Escape Sequences

\a

Alert (bell)

\b

Backspace

\c

Print the line without a newline

\f

Form feed

\n

Newline

\r

Return

\t

Tab

\v

Vertical tab

\\

Backslash

\nnn

The character whose ASCII code is nnn (octal)


The TC shell uses the style of both BSD and SVR4, but allows you to modify the behavior of the echo command by using the built-in echo_style variable; for example, set echo_style=bsd. See Table 9.22, and the manual page for echo.

Table 9.22. The echo_style Variable (SVR4 and BSD)

System

Behavior

bsd

If the first argument is -n, the newline is suppressed

both

Both -n and escape sequences are in effect (the default)

none

Recognizes neither sysv or bsd

sysv

Expands escape sequences in echo strings


Example 9.101.

1   > echo The username is $LOGNAME.

    The username is ellie.

2   > echo "\t\tHello there\c"

                Hello there>

3   > echo -n "Hello there"

    Hello there$

4   > set echo_style=none

5   > echo  "\t\tHello there\c"

    -n \t\tHello there\c


EXPLANATION

  1. The echo command prints its arguments to the screen. Variable substitution is performed by the shell before the echo command is executed.

  2. The echo command by default, supports escape sequences similar to those of the C programming language and used in the SVR4 version of echo. The > is the shell prompt.

  3. With the –n option, echo displays the string without the newline.

  4. The echo_style variable is assigned the value none. Neither the BSD –n switch nor the SVR4 escape sequences are in effect.

  5. With the new echo style, the string is displayed.

9.19.2 The printf Command

The GNU version of printf can be used to format printed output. It prints the formatted string, in the same way as the C printf function. The format consists of a string that may contain formatting instructions to describe how the printed output will look. The formatting instructions are designated with a % followed by specifiers (diouxXfeEgGcs), where %f would represent a floating-point number and %d would represent a whole (decimal) number.

To see a complete listing of printf specifiers and how to use them, type at the command-line prompt printf – –help. (See Table 9.23.) To see what version of printf you are using, type printf – –version.

Table 9.23. Format Specifiers for the printf Command

Format Specifier

Value

\"

Double quote

\0NNN

An octal character in which NNN represents 0 to 3 digits

\\

Backslash

\a

Alert or beep

\b

Backspace

\c

Produce no further output

\f

Form feed

\n

Newline

\r

Carriage return

\t

Horizontal tab

\v

Vertical tab

\xNNN

Hexadecimal character in which NNN is 1 to 3 digits

%%

Single %

%b

ARGUMENT as a string with \ escapes interpreted


FORMAT


printf format [argument...]


Example 9.102.

printf "%10.2f%5d\n" 10.5  25


Example 9.103.

1   > printf --version

    printf (GNU sh-utils) 1.16

2   > printf "The number is %.2f\n" 100

    The number is 100.00

3   > printf "%-20s%-15s%10.2f\n" "Jody" "Savage" 28

    Jody                Savage              28.00

4   > printf "|%-20s|%-15s|%10.2f|\n" "Jody" "Savage" 28

    |Jody                |Savage         |     28.00|


EXPLANATION

  1. The GNU version of the printf command is printed. It is found in /usr/bin.

  2. The argument 100 is printed as a floating-point number with only two places to the right of the decimal point printing, designated by the format specification %.2f in the format string. Note that unlike C, there are no commas separating the arguments.

  3. This time the format string specifies that three conversions will take place: the first one is %–20s (a left-justified, 20-character string), next is %–15s (a left-justified, 15-character string), and last is %10.2f (a right-justified 10-character floating-point number; one of those characters is the period and the last two characters are the two numbers to the right of the decimal point). Each argument is formatted in the order of the corresponding % signs, so that string "Jody" corresponds to first %, string "Savage" corresponds to the second %, and the number 28 to the last % sign. The vertical bars are used to demonstrate the width of the fields.

9.19.3 Curly Braces and Variables

Curly braces insulate a variable from any characters that may follow it. They can be used to concatenate a string to the end of the variable.

Example 9.104.

1   > set var = net

    > echo $var

    net

2   > echo $varwork

    varwork: Undefined variable.

3   > echo ${var}work

    network


EXPLANATION

  1. The curly braces surrounding the variable name insulate the variable from characters that follow it.

  2. A variable called varwork has not been defined. The shell prints an error message.

  3. The curly braces shield the variable from characters appended to it. $var is expanded and the string work is appended.

9.19.4 Uppercase and Lowercase Modifiers

A special history modifier can be used to change the case of letters in a variable.

Table 9.24. TC Shell Case Modifiers

Modifier

What It Does

:a

Apply a modifier as many times as possible to a single word.

:g

Apply a modifier once to each word.

:l

Lowercase the first uppercase letter in a word.

:u

Uppercase the first lowercase letter in a word.


Example 9.105.

1   > set name = nicky

    > echo $name:u

    Nicky

2   > set name = ( nicky jake )

    > echo $name:gu

    Nicky Jake

3   > echo $name:agu

    NICKY JAKE

4   > set name = ( TOMMY DANNY )

    > echo $name:agl

    tommy danny

5   > set name = "$name:agu"

    > echo $name

    TOMMY DANNY


EXPLANATION

  1. When :u is appended to the variable, the first letter in its value is uppercased.

  2. When :gu is appended to the variable, the first letter in each word in the list of values is uppercased.

  3. When :agu is appended to the variable, all letters in its value are uppercased.

  4. When :agl is appended to the variable, all letters in its value are lowercased.

  5. The variable is reset with all letters in its list uppercased.

    Previous Section  < Day Day Up >  Next Section