UNIX,BOLUM 7     |   home

Unix: csh/ksh Comparison

What is in a Shell?
Configuring the Environment
ksh History Mechanism Overview
Environmental Command Comparison
 What is in a Shell?
In the past, departmental systems have supported the C shell (csh) and its derivative tcsh, but we are now encouraging use of the Korn shell (ksh). While there are many differences, for many users the changes will be insignificant. However, users who work at the shell prompt, especially those who like to configure their environments, work with the command history mechanism, use aliases, or make use of shell scripts, will notice many distinctions between the two shells. This document does not discuss any of these differences in detail. It simply lists some of the most frequently encountered distinctions.
This document is divided into three parts. The first part of this document is a brief introduction to the two environment configuration files, .profile and .kshrc. The second part is a short overview of the command history mechanism in the Korn shell. The third part is a command and setting comparison between the C shell and the Korn shell.
 Configuring the Environment
Every shell has one or two files which are used at login to configure the user's environment. In the C shell these are the .login and .cshrc files. The .login file is read in when you first login to the machine and is primarily used to establish global parameters. The .cshrc file establishes the environment settings for each shell process which is executed during the session. In the Korn shell these roles can both be filled by one file, the .profile. On most machines, however, the roles are split up as they are with the C shell: the .profile performs the role of the .login, and the .kshrc (the name of this file may vary from machine to machine, or even from user to user) performs that of the .cshrc. In either shell, each line in both files is simply a command, each of which could be executed by simply typing the command at the prompt.
Korn Shell .profile Files
When you login on any machine using the Korn shell, the shell looks for the .profile file and executes the commands found in it. The following is an example of the default .profile from /usr/local/etc/skel on the departmental server pangea.usask.ca. NOTE: lines beginning with a pound sign (#) are comments and are ignored by the shell.
# /usr/local/etc/skel/local.profile from pangea.usask.ca 98/07/28
# This file is customizable. The .profile is used to set up your session
# when you log in. Only programs which produce output should be placed
# here. Configuration variables, aliases, and the like should be placed in
# the .kshrc file instead. The following line sets the characters for erase
# (destructive backspace), line kill (destructive backspace to beginning of
# line) and interrupt (abort a running program). The caret represents a
# control code, for instance ^C is control-C. ^? is commonly known as DEL.
# If you get garbage on the screen in some programs (such as talk) when you
# backspace, the upper-right-hand corner of your keyboard will only send ^H
# (known as BS). Change ^? to ^H in the line below.

 stty erase ^? kill ^U intr ^C

# The following line tells the KornShell to look at your file .kshrc to
# find environment configurations. You shouldn't change this line.

 export ENV=${HOME}/.kshrc

# If you want to add programs to run each time you log in, add them
# immediately. If you uncommented the line below (by removing the pound
# sign), the date and time would be printed each time you logged in.

# date

# The following line controls whether you want to receive system messages,
# usually from the "talk" program. mesg y will allow messages to come in;
# mesg n will disallow them.

# mesg y
 mesg n > /dev/null 2>&1

# End of .profile.
Korn Shell .kshrc Files
Unlike the C shell, the Korn shell does not automatically look for a second file to configure the environment. It first checks the value of the ENV variable (set in your .profile). This variable may contain the name of a file-- if it does, the Korn shell will try to read in this file and use it to set up environmental settings. You should NOT change the name of this file or move it out of your home directory. If you do, you may lose important settings! The following is the default .kshrc file setup for users from /usr/local/etc/skel/local.kshrc. NOTE: as in the .profile, lines beginning with the pound sign are comments and are ignored by the shell.
# .kshrc from pangea.usask.ca 98/07/28
# This file is customizable. The .kshrc is used to set up the environment
# in which your programs run. PROGRAMS WHICH PRODUCE OUTPUT (even error
# messages) SHOULD NEVER GO IN THIS FILE!

# Load the standard system-wide configuration file.  If you want to change
# the defaults set in the file /usr/local/etc/skel/local.kshrc
# you should do so below. Please do not remove the following lines -
# if you don't load the file, you won't get the system updates!

if [[ -r /usr/local/etc/skel/local.kshrc ]]
then
     . /usr/local/etc/skel/local.kshrc
fi

# Variables are set in the Korn Shell differently to the C-Shell you may
# be used to.  If you had a C-Shell line of the form
#
#   setenv VARIABLE value
#
# the Korn Shell equivalent would be
#   export VARIABLE=value
# It is more efficient if you have multiple lines, to save the export
# until the end, for instance:
#
#   VAR1=val1
#   VAR2=val2
#   export VAR1 VAR2
#
# Aliases are also set differently from the C-Shell. A C-Shell alias
#
#   alias newcommand 'old command'
#
# has the Korn Shell equivalent
#
#   alias newcommand='old command'
#
# Unlike C-Shell, you can't use "arguments" (like $*) in an alias;
# functions are used instead. For example:
#
# promptcmd() { PS1=`hostname`:`pwd`"$ "; export PS1; }
# cwdcmd() { `cd` $*; promptcmd; }
# alias cd=cwdcmd
#
# The file /usr/local/etc/skel/kshrc contains the default environment,
# and you may want to look at it for examples of how Korn Shell works.
# You can read that file (for example, with "more" or "pico"). If you wish
# to change one of the defaults, or to add your own configurations, do so
# below.  If you make changes, you may want to try saving this file and
# telnetting in from another window to be sure it works; if you log out
# and you made a mistake below, you may not be able to log back in!!
 ksh History Mechanism Overview
The Korn shell history mechanism works slightly differently than that in the C shell. The command history can be viewed with the command
$ fc -l
i This command is actually aliased as `history', which lets you use the same command as in the C shell to view your history list. Once you have printed the history list, some versions of the Korn shell let you selectively reexecute commands by typing
$ fc -s cmdnumber
The Korn shell also incorporates into its history mechanism a way to interactively scroll through the history list, edit commands, and execute them. You can do this with the command:
$ set -o vi
or
$ set -o emacs
This lets you use the commands associated with the "vi" and "emacs" editors, respectively, to scroll through and edit your recent commands. On departmental systems, the arrow keys will not work for history recall if you use set -o vi. The reason is thatthe arrow keys are mapped in /usr/local/etc/skel/local.kshrc to support the emacs key mappings.
 Environmental Command Comparison
Each line in the following table has two parts: the first shows a C shell command or construct, the second the closest corresponding element from the Korn shell. Users needing more detailed explanations should refer to the Korn Shell online man pages. NOTE: Users should be aware that the vast majority of the most common user commands are identical across shells, e.g. cd, pwd, ls, cat, who, finger, etc.

Function or Purpose
C Shell Command/Construct
Korn Shell Command/Construct
File containing global settings
 .login
 .profile
File containing original settings copied in for subprocesses
 .cshrc
 .kshrc
read in and execute list of commands from filename
 % source filename
 $ .filename
locate copy of command in path to be executed
 % which command
 $ whence command
initialize variable varname to value
 set varname value
 varname=value
initialize variable varname to value and make available to subprocesses
 setenv varname value
 export varname=value
initialize array variable arrayname to list of values value1, etc.
 set arrayname (v1 v2 v3...)
 set-A arrayname v1 v2 v3...
create alias aliasname for command
 alias aliasname command
 alias aliasname=command
print command history list
 history
 history     or     fc -l
reexecute most recent command
 !!
 r