Features Common to the Bourne Again and TC Shells


Most of the features common to both bash and tcsh are derived from the original C Shell:

  • Command line expansion (also called substitution; page 341)

  • History (page 342)

  • Aliases (page 344)

  • Job control (page 345)

  • Filename substitution (page 346)

  • Directory stack manipulation (page 346)

  • Command substitution (page 346)

Because the chapters on bash discuss these features in detail, this section focuses on the differences between the bash and tcsh implementations.

Command Line Expansion (Substitution)

Refer to "Processing the Command Line" on page 321 for an introduction to command line expansion in the Bourne Again Shell. The tcsh man page uses the term substitution instead of expansion, which is used by bash. The TC Shell scans each token for possible expansion in the following order:

  1. History substitution (page 342)

  2. Alias substitution (page 344)

  3. Variable substitution (page 353)

  4. Command substitution (page 346)

  5. Filename substitution (page 346)

  6. Directory stack substitution (page 346)

History

The TC Shell assigns a sequential event number to each command line. You can display this event number as part of the tcsh prompt (refer to "prompt" on page 360). Examples in this section show numbered prompts when they help illustrate the behavior of a command.

history Builtin

As in bash, the tcsh history builtin displays the events in your history list. The list of events is ordered with the oldest events at the top. The last event in the history list is the history command that displayed the list. In the following history list, which is limited to ten lines by the argument of 10 to the history command, command 23 modifies the tcsh prompt to display the history event number. The time each command was executed appears to the right of the event number.

32 $ history 10     23  23:59   set prompt = "! $ "     24  23:59   ls -l     25  23:59   cat temp     26  0:00    rm temp     27  0:00    vim memo     28  0:00    lpr memo     29  0:00    vim memo     30  0:00    lpr memo     31  0:00    rm memo     32  0:00    history 


History Expansion

The same event and word designators work in both shells. For example, !! refers to the previous event in tcsh, just as it does in bash. The command !328 executes event number 328 and !?txt? executes the most recent event containing the string txt. For more information refer to "Using an Exclamation Point (!) to Reference Events" on page 299. Table 9-1 lists the few tcsh word modifiers not found in bash.

Table 9-1. Word modifiers

Modifier

Function

u

Converts the first lowercase letter into uppercase

l

Converts the first uppercase letter into lowercase

a

Applies the next modifier globally within a single word


You can use more than one word modifier in a command. For instance, the a modifier, in combination with the u or l modifier, enables you to change the case of an entire word.

tcsh $ echo $VERSION VERSION: Undefined variable. tcsh $ echo !!:1:al echo $version tcsh 6.12.00 (Astron) 2002-07-23 (powerpc-apple-darwin) options 8b,... 


In addition to using event designators to access the history list, you can use the command line editor to access, modify, and execute previous commands (page 350).

Variables

The variables that you set to control history in tcsh are different from those used in bash. Whereas bash uses HISTSIZE and HISTFILESIZE to determine the number of events that are preserved during and between sessions, tcsh uses history and savehist Table 9-2 for these purposes.

Table 9-2. History variables

Function

Variable

Default

Maximum number of events saved during a session

history

100 events

Location of the history file

histfile

~/.history

Maximum number of events saved between sessions

savehist

not set


history and savehist

When you exit from a tcsh shell, the most recently executed commands are saved in your ~/.history file. The next time you start the shell this file initializes the history list. The value of the savehist variable determines the number of lines saved in the .history file (not necessarily the same as the history variable). If savehist is not set, tcsh does not save history between sessions. The history and savehist variables must be local (declared with set, not setenv). The history variable holds the number of events remembered during a session and the savehist variable holds the number remembered between sessions. See Table 9-2.

If you set the value of history too high, it can use too much memory. If it is unset or set to zero, the shell does not save any commands. To establish a history list of the 500 most recent events, give the following command manually or place it in your ~/.tcshrc startup file:

tcsh $ set history = 500 


The following command causes tcsh to save the 200 most recent events across login sessions:

tcsh $ set savehist = 200 


You can combine these two assignments into a single command:

tcsh $ set history=500 savehist=200 


After you set savehist you can log out and log in again, and the 200 most recent events from the previous login sessions will appear in your history list. Set savehist in your ~/.tcshrc file if you want to maintain your event list from login to login.

histlit

If you set the variable histlit (history literal), history displays the commands in the history list exactly as they were typed in without any shell interpretation. The following example shows the effect of this variable (compare the lines numbered 32):

tcsh $ cat /etc/csh.cshrc ... tcsh $ cp !!:1 ~ cp /etc/csh.cshrc ~ tcsh $ set histlit tcsh $ history ...    31  9:35    cat /etc/csh.cshrc    32  9:35    cp !!:1 ~    33  9:35    set histlit    34  9:35    history tcsh $ unset histlit tcsh $ history ...    31  9:35    cat /etc/csh.cshrc    32  9:35    cp /etc/csh.cshrc ~    33  9:35    set histlit    34  9:35    history    35  9:35    unset histlit    36  9:36    history 


Optional

There is a difference in how bash and tcsh expand history event designators. If you give the command !250w, bash replaces it with command number 250 with a character w appended to it. In contrast, tcsh looks back through your history list for an event that begins with the string 250w to execute. The reason for the difference: bash interprets the first three characters of 250w as the number of a command, whereas tcsh interprets those characters as part of the search string 250w. (If the 250 stands alone, tcsh TReats it as a command number.)

If you want to append w to command number 250, you can insulate the event number from the w by surrounding it with braces:

!{250}w 



Aliases

The alias/unalias feature in tcsh closely resembles its counterpart in bash (page 311). However, the alias builtin has a slightly different syntax:

alias name value 


The following command creates an alias for ls:

tcsh $ alias ls "ls -lF" 


The tcsh alias lets you substitute command line arguments, whereas bash does not:

$ alias nam "echo Hello, \!^ is my name" $ nam Sam Hello, Sam is my name 


The string \!* within an alias expands to all command line arguments:

$ alias sortprint "sort \!* lpr" 


The next alias displays its second argument:

$ alias n2 "echo \!:2" 


Special Aliases

Some alias names, called special aliases, have special meaning to tcsh. If you define an alias with one of these names, tcsh executes it automatically as explained in Table 9-3. Initially all special aliases are undefined.

Table 9-3. Special aliases

Alias

When executed

beepcmd

Whenever the shell would normally ring the terminal bell. Gives you a way to have other visual or audio effects take place at those times.

cwdcmd

Whenever you change to another working directory.

periodic

Periodically, as determined by the number of minutes in the tperiod variable. If tperiod is unset or has the value 0, periodic has no meaning.

precmd

Just before the shell displays a prompt.

shell

Gives the absolute pathname of the shell that you want to use to run scripts that do not start with #! (page 267).


To see a list of current aliases, give the command alias. To view the alias for a particular name, give the command alias followed by the name.

History Substitution in Aliases

You can substitute command line arguments by using the history mechanism, where a single exclamation point represents the command line containing the alias. Modifiers are the same as those used by history (page 299). In the following example, the exclamation points are quoted so that the shell does not interpret them when building the aliases:

21 $ alias last echo \!:$ 22 $ last this is just a test test 23 $ alias fn2 echo \!:2:t 24 $ fn2 /Users/jenny/test /Users/alex/temp /Users/barbara/new  temp 


Event 21 defines for last an alias that displays the last argument. Event 23 defines for fn2 an alias that displays the simple filename, or tail, of the second argument on the command line.

Job Control

Job control is similar in both bash (page 272) and tcsh. You can move commands between the foreground and background, suspend jobs temporarily, and get a list of the current jobs. The % character references a job when followed by a job number or a string prefix that uniquely identifies the job. You will see a minor difference when you run a multiple-process command line in the background from each shell. Whereas bash displays only the PID number of the last background process in each job, tcsh displays the numbers for all processes belonging to a job. The example from page 272 looks like this under tcsh:

tcsh $ find . -print | sort | lpr & grep -l alex /tmp/* > alexfiles & [1] 18839 18840 18841 [2] 18876 


Filename Substitution

The TC Shell expands the characters *, ?, and [] in a pathname just as bash does (page 133). The * matches any string of zero or more characters, ? matches any single character, and [] defines a character class, which is used to match single characters appearing within a pair of brackets.

The TC Shell expands command line arguments that start with a tilde (~) into filenames in much the same way that bash does (page 348), with the ~ standing for the user's home directory or the home directory of the user whose name follows the tilde. The bash special expansions ~+ and ~ are not available in tcsh.

Brace expansion (page 323) is available in tcsh. Like tilde expansion, it is regarded as an aspect of filename substitution even though brace expansion can generate strings that are not the names of actual files.

In tcsh and its predecessor csh, the process of using patterns to match filenames is referred to as globbing and the pattern itself is called a globbing pattern. If tcsh is unable to identify one or more files that match a globbing pattern, it reports an error (unless the pattern contains a brace). Setting the shell variable noglob suppresses filename substitution, including both tilde and brace interpretation.

Manipulating the Directory Stack

Directory stack manipulation in tcsh does not differ much from that in bash (page 275). The dirs builtin displays the contents of the stack, and the pushd and popd builtins push directories onto and pop directories off of the stack.

Command Substitution

The $(...) format for command substitution is not available in tcsh. In its place you must use the original '...' format. Otherwise, the implementation in bash and tcsh is identical. Refer to page 327 for more information on command substitution.




A Practical Guide to UNIX[r] for Mac OS[r] X Users
A Practical Guide to UNIX for Mac OS X Users
ISBN: 0131863339
EAN: 2147483647
Year: 2005
Pages: 234

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net