History

Version

Two versions:

  1. /bin/csh: The original BSD version of csh.
  2. /bin/tcsh: An improved version of csh called tcsh.

Login

When you login the shell is started in your home directory and commands are read from .cshrc and .login

Functionality

Command Line Arguments

Special Variables:

Reading Input from User

$<: Reads a line from stdin up to but not including newline.

Example

#!/bin/csh
echo "Name: "
set name=$<
echo Greetings, $name

Basic Operators

OperatorDescription
()Grouping
!Logical note
> >= < <=Greater than, less than
&&Logical and
=~String matches
!~String does not match

Control Structures

if (expression) command
if (expression) command then
    command
endif
if (expression) command then
    command
else if (expression) then
else
    command
endif

Example

if ($#arv < 2) then
    echo "Too few arguments"
    exit
endif

File Queries

Arithmetic (@)

#!/bin/csh
if ( $#argv == 0 ) then
    echo -n "Enter time in minutes: "
    @ min = $<
else
    @ min = $1
endif
@ sec = $min * 60
echo$min minutes is $sec seconds”

Switch Case

#!/bin/csh -f
# Script name: colors
echo -n "Which color do you like? "
set color = $<
switch ("$color")
case bl*:
    echo I feel $color
    echo The sky is $color
    breaksw
case red:
     # Is is red or is it yellow?
    case yellow:
    echo The sun is sometimes $color.
    breaksw
default:
    echo $color not one of the categories.
    breaksw
endsw

Looping Constructs

Predetermined Iterations:

foreach name ( wordlist )
    commands
end

Condition-based Iterations:

while ( expression )
    commands
end

Example: foreach

set list = ( one two three )
foreach word ( $list )
  echo $word
end

Example: while

#! /bin/csh
@ var = 5
while ( $var > 0 )
  echo $var
  @ var --
end

Loop Control

Quoting

eval

eval: Evaluates string and executes the result.

Example

set x=23
set y=x
eval echo \$$y

Debugging

% csh –n scriptname
% csh –v scriptname
% csh –x scriptname

Note: Flags can also be added to shebang