Name

csh - Command shell

Syntax

csh [ -c ] [ command ]

Description

Csh is a command interpreter. Without any arguments, csh goes through a normal startup procedure, eventually giving you a command line where you can enter more commands.

If csh is invoked with a command argument, it processes that command and exits when finished. If the -c flag is included, csh processes the command, but does not exit. If only -c is given, the shell immediately presents a command line for entering commands (it does not run your login script, described later).

About The Shell


Csh is the central program from which all other programs can be accessed. It is often called a command interpreter, acting as a liaison between you and the system. To instruct the system to perform a task, you enter your request on a command line. The shell processes the command line to carry out your request by means of a built-in shell function, or by selecting a program external to the shell to complete the request. The shell contains many internal functions for use in the shell environment. There are even more external programs that can be called upon from the shell. After a command line is processed, the shell prompts you for additional commands until you’re ready to quit.

The shell can also process a series (or batch) of commands contained in a file, as if they had been entered by hand on the command line. Files containing shell commands are called shell scripts. Scripts use special features to control the execution of commands in a way that reduces arduous tasks into simple, automated procedures. More on scripts later.

The Command Line


Executing a built-in or external command from the shell is simple — just type in the command’s name. While typing in a command line, various editing keystrokes are available:

^X

cancel what you’ve entered so far

^H/DEL

rubout the character left of the cursor

^W

rubout the word left of the cursor

Command Line History


The shell maintains a history of the 20 most recently entered commands. You can scroll through command lines entered and repeat one by pressing Control-P (for previous) to move backward in time toward the oldest command lines. Pressing Control-N (for next) moves forward to the most recent command lines.

The shell also recognizes the exclamation point (!) character at the beginning of each line as a way of referencing specific history lines.

!!

Selects the last entered command line.

!#

Selects a command line by the number associated with #.

!text

Selects the most recent command line that begins with a pattern of text.

Arguments and Special Characters


In many cases, you may need to give extra information, or arguments, to a command. To help the shell determine which items are commands and arguments, separate them with space characters.

To specify a single argument that may contain spaces within it, enclose the argument in quotation marks. Example:

echo "This is one argument"

The shell ignores most control characters entered on the command line. To insert a control character, use a caret before the corresponding letter (e.g., “^G” for Control-G).

If you need to enter a quotation mark or caret in an argument, prefix it with a backslash “\” character. Likewise, to enter a backslash, prefix it with a backslash (in other words, two backslash characters make one). Examples:

echo "\"Wow!\" she exclaimed." echo "Here is a caret: \^" echo "Here is a backslash: \\"

Using a backslash to insert special characters is known as escaping. There are other characters the shell recognizes as having special meanings, such as the dollar sign ($), tilde (~), period (.), and asterisk (*). These are discussed later.

Built-In Commands


The shell has many built-in commands. They are listed below along with the arguments they require. Optional arguments are shown between [ brackets ]. Arguments shown with “…” following means “one or more of the same”.

add src… [targ]

Adds source file(s) onto the end of the target file. If one file argument is given, the file must reside outside of the current directory and will be appended to a file in the current directory with the same name. If more than one argument is given, the final argument is the target file, and all others are appended onto it.

alias [old new…]

Substitutes a command name with a new name (and any additional arguments). The new replacement arguments may contain the following special characters: !^ (inserts the first argument from the command line here), !$ (inserts the last argument here), and !* (inserts all arguments here). Entering alias without any arguments displays a list of the current aliases.

cat file…

Short for “concatenate”. Cat is normally used to display the contents of a file, but is also useful for redirecting a file to other files or devices (e.g. a printer).

cd [dir]

Changes the current working directory. If the argument is omitted, the current directory becomes your $home directory.

clear

Clears the screen.

cp src… [targ]

Copies a file. If only one file is given, it must reside outside of the current directory, and is copied to a file of the same name within the current directory. If more than one argument is given, the source files are copied onto the target file. If the target is a directory, the files are copied into the directory, retaining their names.

echo [-n] args…

Echoes any arguments that follow. If the -n option is given, no newline is printed after the final argument. To redirect output from echo to a file, include >file as the last argument. To append output to an existing file, use >>file.

exec command…

This replaces the current shell with a new command interpreter. If called from a shell script, the script is terminated.

exit [n]

Tells the shell to quit and return to the calling process. Typing Control-D is recognized as the exit command, too. The optional number argument useful for returning a result code to the calling process (default is zero).

history

Displays the 20 most recent command lines.

if expr then […]

Conditional test to execute commands. This is normally used within shell scripts and is discussed later.

logout

Terminate connection immediately.

mkdir dir…

Makes a subdirectory.

mv src… [targ]

Moves a file from one place to another. It works identically to cp, except it deletes the source file after copying it to the target. It is also used to rename a file if both src and targ reside in the same directory.

pwd

Prints out the name of the current Working Directory.

read var

Reads input from the user and assigns it to a variable.

rm file…

Removes files and empty directories.

set [var=value…]

Sets shell variables to specified values. Set without arguments displays the currently set variables.

shift [var]

Shifts the arguments in the command line down by removing the first argument. Without arguments, shift affects argv (see the discussion on variables later for details). By including an optional variable name, that variable is affected. This command is typically used by shell scripts for argument parsing.

source file

Executes shell commands from a text file.

unalias name…

Removes aliases.

unset var…

Removes variables.

Many more commands can be called up from disk by the shell. One such command is ls, which lists the contents of a directory. To see the names of all the disk-based commands, enter:

ls $path

Entering a command without any arguments usually displays a line of “usage” information. You can get more information about a command by using the man command (short for “manual”). Man requires the name of the item you want more help on as an argument. It prints out nicely formatted pages from the online manual. (Try the “man man” command — it lets man tell you more about itself).

Access Permissions


The shell watches certain requests to make sure the user has the required access permissions. There are four types of file access: reading from a file, writing to or creating a file, destroying a file, and executing a file. Under ProLine, access permissions are specified for the entire contents of a single directory level, rather than on a file-by-file basis. (See dstat for more details).

Output Redirection


If you’re using the system from the local console, you can redirect the output from the shell and other programs to a printer attached to the computer. To do this, include >.printer as the final argument to a command line. This tells the shell to turn on printer output. It’s only good for one command, so there is no additional command to turn it off; it will do so when it wants another command from you.

Example:

echo This is a test >.printer

Another form of output redirection is to send output into a file, rather than to the console or printer. The syntax is similar to >.printer.

Example:

man csh >temp

In this case, it is man’s job to handle redirecting its output into the file called “temp” (or whatever name you provide). Unfortunately, not all programs on the system provide this ability. Check with man to see if a certain command provides file output redirection.

Directory Shorthand


The shell looks at the arguments you give to see if any start with special characters that reference directories. These characters are one or two periods (.), and the tilde (~).

When the shell encounters a single period, it replaces it with the path to the current working directory. If it sees two periods, it replaces them with the name of the parent directory of the current working directory. In other words, two periods means to step back one directory level. So, if a single periods refers to $/usr/jholt, then two periods refer to $/usr.

The tilde (~) is directory shorthand for referencing home directories. To access a file in your home directory called stuff when the current directory is not your home, use “~/file”. The shell sees “~/” and knows that you’re referencing your home directory (which is shorthand for “$/usr/yourloginname/file”). By introducing the login name of another user between ~ and /, you reference that user’s home directory. For example, “~jsbach/sonata” is short for $/usr/jsbach/sonata.

Shell Variables


Shell variables are short names given to command line items that are cumbersome or hard to remember. The shell comes with a number of preset variables containing useful information. To obtain the value of a variable, a dollar sign ($) is placed before its name (e.g. $path)

Variables the shell defines for you are:

/

Directory in which ProLine resides on the local file system. Since one ProLine system may have a startup prefix that is different from another ProLine, the $/ variable allows ProLine systems to work consistently in the shell from one system to the next.

argc

Contains the count (+1) of the arguments passed to a shell script.

argv

Contains the shell script’s name and arguments passed.

caller

The system caller ID number.

cwd

The current working directory.

date

The current date and time in the standard ProLine date format:

Sat, 18 Apr 92 10:22:19 PDT

editor

The name of the editor used to edit text files on ProLine.

exit

If set, any errors in a shell script cause the script to halt. By unsetting this variable, errors will allow a script to continue processing.

gid

Your group-ID number.

home

Your home directory.

host

The name of the host system.

login

Your login name.

mail

The path to your mailbox file.

name

Your first and last name.

path

A list of paths to directories containing external command files.

prompt

Your shell prompt.

shell

Path to the shell program.

shlvl

The number of nested shell levels.

status

The result of the last command.

time

The current day of the week, date, month, year, hours, minutes, seconds, and day of week number in this format:

Sat 18 04 92 10 22 19 6

Month values are 01 for Jan up to 12 for Dec. Day of week values are 0 for Sun up to 6 for Sat. All numeric values (except for day of the week number) are padded with zeroes to fill out to two digits.

tmpdir

Path to a directory for temporary file storage (usually a RAM disk or fast hard disk volume).

tty

Contains “modem” or “console” depending on how you logged in — remotely or locally.

uid

Your user-ID number.

version

The shell’s version information.

Subscripted Variables


You can access individual words within a variable by including a subscript after the variable’s name. For example, to access the first word of the $time variable (the day of the week), use $time[0]. To access the hours, use $time[4]. To access arguments passed to a script from within the script, use $argv[x] as appropriate.

Avoiding Variable Expansion


The shell always expands variables to the values they contain, even when placed inside quotation marks. To cause the shell to pass a dollar sign and a word through unexpanded, prefix it with a backslash. (e.g. “\$time”). Consider the following:

alias bye "echo Login: $date[4]^MLogout: \$date[4]; logout"

In this alias definition, “bye” is defined with an echo command that includes the $date variable in two places. The first instance is not escaped by a backslash, and therefore when this alias command is executed, the current time is inserted. The second instance of $date is escaped by a backslash, and therefore the word “$date” is literally inserted into the definition. When the bye alias is actually used, only then would the second instance of $date be recognized (since the backslash would not be present), and the current time inserted. As a result, the bye alias displays both the login and logout times before disconnecting.

The same is true when assigning variables to a variable. If you want your shell prompt to display the current working directory, use:

set prompt="\$cwd> "

If the $cwd variable reference had not been escaped with a backslash, this command would assign the prompt variable a value containing the current working directory at the time when set was issued. The prompt would never change even as new directories were chosen.

Shell Scripts


You can instruct the shell to execute more than just one command at a time by entering command sequences separated by semicolons (;) on the same line. But, if you need to execute many, often repeated command sequences, it is best to put them into a shell script.

A shell script contains any commands you would enter on the command line. Once created using any text editor, you can execute the script by using the source command. (To make a script executable without having to use source, change its file type to CMD with the setfile command).

Initialization Scripts


When first launched, csh sets up an internal environment which can be further customized through various initialization scripts. If it exists, csh invokes a system-wide initialization script $/etc/cshrc. This script is set up by the administrator to perform various sign-on tasks, like reporting new system bulletins (news), reporting mail, and finally invoking the user’s personal login script (discussed next).

When interactive subshells are launched without arguments, csh does not execute $/etc/cshrc nor the user’s login script, but will attempt to execute the user’s personal cshrc file, if it exists in the home directory.

The $/etc/cshrc Script


The system-wide startup script, cshrc, is expected to reside in $/etc or the configured temporary files directory ($tmpdir), which might be a high-speed RAM disk. If the system-wide cshrc does not exist, csh attempts to execute the user’s login script during a login session.

The Login Script


Each user has a special script of startup commands. The script is called login (~/login). This file can contain a variety of shell commands, and can be modified as desired, allowing a unique and personal entry into the system for each session. Once the login script has done its job, the shell may prompt for further commands, but typically login invokes the user’s personal cshrc file.

The login script is derived originally from the default script ($/etc/default/login) when an account is first created.

Personal cshrc Script


The personal cshrc file (~/cshrc), contains commands that are used to install aliases and set variables such that each shell is set up as the user desires. This script is normally launched by a command in the user’s ~/login script, but is automatically run whenever csh invokes an interactive subshell.

Script Control Using the IF Directive


To control execution of script commands, use the if directive. If, and its partners else and endif, are used to test certain conditions and execute only the commands which should be processed based on the results of those conditions. The condition is determined by the logical expression given to if.

An example:

if $a = $b then echo TRUE else echo FALSE

Another example:

if $time[0] = "Sun" and -f workfile then echo It is Sunday AND the workfile exists! else echo EITHER it is not Sunday echo OR the workfile does not exist. endif

For more details on if statements, see the if(C) manual entry.

Files

$/bin/csh
C-Shell initialization program,
$/etc/cshrc
system-wide initialization script,
$/bin/cshx
C-Shell kernel code,
$/usr/*
your “home” directory (~),
~/login
your login startup script,
~/cshrc
your shell initialization script,
$/sys/modules/parse
parsing tools,
$tmpdir/cshenv*
temporary environment file,
$tmpdir/*
C-Shell runs faster when $/bin/cshx, $/etc/cshrc, and

$/sys/modules/parse live on a RAM disk.

Author

Morgan Davis (mdavis@mdg.cts.com)

See Also

if(C), plush(C), rc(ADM), scripts(M)