Previous Section  < Day Day Up >  Next Section

9.13. New Features of the Interactive TC Shell

The TC shell is a public domain enhanced version of its predecessor, the Berkeley UNIX C shell. If you are using Linux, you will have this available to you rather than the traditional C shell. Although tcsh is included in most Linux distributions, it can also be ported to a number of other operating systems, including Solaris, Windows NT, HP-UX, QNX, and more. There are an assortment of new features in the TC shell. The remainder of this chapter covers these additional features, most of them shortcuts, including command-line editing, fancy prompts, programmable completions (filenames, commands, and variables), spelling correction, and so forth.

This section focuses only on the new features added to the TC shell. The topics covered in the last section apply to both the C shell and TC shell.

9.13.1 Versions of tcsh

To find out what version of tcsh you are using, type at the shell prompt:


which tcsh


To tell you in what directory tcsh is installed (normally /bin), and to print the version information, type:


/directory_path/tcsh -c 'echo $version'


Example 9.69.

1   which tcsh

    /bin/tcsh

2   /bin/tcsh -c 'echo $version'

    tcsh 6.07.09 (Astron) 1998-07-07 (i386-intel-linux) options

    8b,nls,dl,al,rh,color


9.13.2 The Shell Prompts

The TC shell has three prompts: the primary prompt, a > symbol; the secondary prompt, a question mark (?) followed by a tcsh command such as while, foreach, or if; and a third prompt used for the spelling correction feature. The primary prompt is the prompt that is displayed on the terminal after you have logged in. It can be reset. If you are writing scripts at the prompt that require tcsh programming constructs, for example, decision making or looping, the secondary prompt will appear so that you can continue on to the next line. It will continue to appear after each newline, until the construct has been properly terminated. The third prompt appears to confirm automatic spelling correction if spelling correction is turned on. (See "TC Shell Spelling Correction" on page 490.) It contains the string CORRECT > corrected command (y|n|e|a)?. The prompts can be customized by adding special formatting sequences to the prompt string. See Table 9.9.

Table 9.9. Prompt Strings

String

Description

%/

The current working directory

%~

The current working directory, where ~ represents the user's home directory and other users' home directories are represented by ~user

%c[[0]n], %.[[0]n]

The trailing component of the current working directory, or if n (a digit) is given, n trailing components

%C

Like %c, but without ~ substitution

%h, %!, !

The current history event number

%M

The full hostname

%m

The hostname up to the first "."

%S (%s)

Start (stop) standout mode

%B (%b)

Start (stop) boldfacing mode

%U (%u)

Start (stop) underline mode

%t, %@

The time of day in 12-hour AM/PM format

%T

Like %t, but in 24-hour format

%p

The "precise" time of day in 12-hour AM/PM format, with seconds

%P

Like %p, but in 24-hour format

^c

c is parsed as in bindkey

\c

c is parsed as in bindkey

%%

A single %

%n

The user name

%d

The weekday in "Day" format

%D

The day in "dd" format

%w

The month in "Mon" format

%W

The month in "mm" format

%y

The year in "yy" format

%Y

The year in "yyyy" format

%l

The shell's tty

%L

Clears from the end of the prompt to the end of the display or the end of the line

%$

Expands the shell or environment variable name immediately after the $

%#

> (or the first character of the promptchars shell variable) for normal users, # (or the second character of promptchars) for the superuser

%{string%}

Includes string as a literal escape sequence; should be used only to change terminal attributes and should not move the cursor location; cannot be the last sequence in prompt

%?

The return code of the command executed just before the prompt

%R

In prompt2, the status of the parser; in prompt3, the corrected string; in history, the history string


The Primary Prompt.

When running interactively, the prompt waits for you to type a command and press the Enter key. If you do not want to use the default prompt, reset it in the .tcshrc file and it will be set for this and all TC shells subsequently started. If you only want it set for this login session, set it at the shell prompt.

Example 9.70.

1   > set prompt = '[ %n@%m %c]# '

2   [ ellie@homebound ~]#  cd ..

3   [ ellie@homebound /home]#  cd ..


EXPLANATION

  1. The primary prompt is assigned the user's login name (%n), followed by the hostname (%m), a space, and the current working directory. The string is enclosed in square brackets, followed by a #.

  2. The new prompt is displayed. The ~ appearing in the prompt represents the user's home directory. The cd command changes directory to the parent directory.

  3. The new prompt indicates the current working directory, /home. In this way the user always know what directory he or she is in.

The Secondary Prompt.

The secondary prompt appears when you are writing online scripts at the prompt. The secondary prompt can be changed. Whenever shell programming constructs are entered, followed by a newline, the secondary prompt appears and continues to appear until the construct is properly terminated. Writing scripts correctly at the prompt takes practice. Once the command is entered and you press Enter, you cannot back up, and the tcsh history mechanism does not save commands typed at the secondary prompt.

Example 9.71.

1      > foreach pal (joe tom ann)

2      foreach? echo Hi $pal

3      foreach? end

       Hi joe

       Hi tom

       Hi ann

4      >


EXPLANATION

  1. This is an example of online scripting. Because the TC shell is expecting further input after the foreach loop is entered, the secondary prompt appears. The foreach loop processes each word in the parenthesized list.

  2. The first time in the loop, joe is assigned to the variable pal. The user joe is sent the contents of memo in the mail. Then next time through the loop, tom is assigned to the variable pal, and so on.

  3. The end statement marks the end of the loop. When all of the items in the parenthesized list have been processed, the loop ends and the primary prompt is displayed.

  4. The primary prompt is displayed.

Example 9.72.

1      > set prompt2='%R %% '

2      > foreach name ( joe tom ann )

3      foreach % echo Hi $name

4      foreach % end

       Hi joe

       Hi tom

       Hi ann

5      >


EXPLANATION

  1. The secondary prompt, prompt2, is reset to the formatted string where %R is the name of the conditional or looping construct entered in line 2 at the primary prompt. The two percent signs will evaluate to one percent sign.

  2. The foreach command has been started. This is a looping construct that must end with the keyword end. The secondary prompt will continue to appear until the loop is properly terminated.

  3. The secondary prompt is foreach %.

  4. After the end keyword is typed, the loop executes.

  5. The primary prompt reappears awaiting user input.

    Previous Section  < Day Day Up >  Next Section