10. tc Shell (Part 1)

tc Shell


  • The tcsh is a new version of the C shell, which originated from Unix BSD
  • The C shell and tcsh base much of their syntax on the C language
  • From the beginning, the Bourne shell and the C shell have had different features and syntax
  • However bash and tcsh now share many of the same features and their syntax are more similar, but there is a bigger difference between bash and tcsh than bash and ksh
  • In this note, we discuss the differences between tcsh and bash

Running the TC Shell

  • tcsh can be the default login shell, just like bash or ksh
  • The default tcsh prompt (stored in a variable called prompt) is: $
  • From any shell, to start tcsh: tcsh
  • To check where tcsh is in the system: whereis tcsh
    • Normally tcsh is at: /bin/tcsh
  • If tcsh is started as a child process of another shell, to get back to the original shell, use: exit
  • To find the current shell that is running: echo $0

Input / Output

  • To print to screen, use echo (just as with bash)
  • To read from the input stream, use set and $<
  • An example script:


echo -n "Your name: "

set name = "$<" # read input line into name

echo “hello $name” # print name

  • Just like with any other shell, tcsh script also accepts command line arguments and options
  • Command line arguments are accessed through the positional parameters(see discussion of positional variables in slide 7)
  • We can also use redirection of both input and output of the shell
  • Redirection of error is not possible without also redirection of output A common way to redirect error is:

( command > outputfile ) >& errfile

  • We can also use command substitutions and pipes for input and output. Command substitutions typically use the ` ` (back ticks) format.

User Defined Variables

  • There are 2 ways to create and initialize variables, depending on whether the data is a string or a number
  • Create and initialize string variables with set
  • Format: set varName = value
    • where space before and after the = is okay
    • For example: set class = “cis18c”
  • Due to the fickle nature of the tcsh parser, always quote strings and string variables, especially if the string has space and punctuation characters
  • Create and initialize numeric variables with @
  • Format: @ varName = value

where space before and after the = is okay

and there is a space between @ and the variable name

For example: @ num = 10

  • $?varName returns 1 if variable is set, 0 if variable is undefined


  • Create and initialize arrays with set and ( )
  • Format: set arrayName = (val1 val2 val3)
  • Array elements are accessed by using [ ] but indexing starts at 1, not 0
  • Multiple index values or * can be put in between [ ] to access multiple elements at a time
  • Example: set colors = (red orange yellow green blue)

echo $colors[1] print: red

echo $colors[2-4] print: orange yellow green

echo $colors[*] print all elements

  • Note: there is no space when using index values inside [ ]

echo $colors[1] is okay, but echo $colors[ 1 ] is not okay

  • $arrayName refers to the entire array
  • $#arrayName returns the number of elements in an array
  • shift arrayName shifts left the elements of the array and the array is shorter by one

Shell Variables

  • Command line arguments are in positional variables ($0, $1, $2, $*) but there is no $@
  • There is no getopts function that can be used for option validation
  • Following the tradition of the C language, command line options and arguments are also stored in the argv array, and this is the common way to access them in a tcsh script

$argv[1] first input argument

$argv[2] second input argument

$argv[n] nth input argument

$argv entire list of arguments

$#argv number of input arguments

  • Process ID is stored in the variable $, just as in bash
  • Exit status is stored in the variable status or ?

Environment Variables

  • System environment variables:
  • Some environment variables can be in lowercase
  • Some common environment variables:

prompt primary shell prompt

home or HOME home directory

path or PATH search path

user or USER user id

shell current shell

0 current shell


  • Arithmetic expressions
    • Operators are similar to bash: +, -, *, /, +=, /= , ++…
    • For the shell to evaluate the expression, the expression must begin with @
    • For example: @ num = 2 * $var num is 2 times var

or @ count++ count is incremented by 1

  • Logical expressions
    • Operators are similar to bash: !, &&, ||
  • Relational expressions
    • Operators are similar to bash: <, >, <=, >=, !=, ==
    • These logical operators apply to string comparison as well as numeric comparison. Depending on the data in the variables, the operation can be a string compare or a numeric compare
    • When using == or != with strings, the operator requires an exact match

String matching

  • Strings can also be partially matched with the operators
    • =~ (for match) and !~ (for not match)
  • The right side of =~ and !~ are wildcards, not regex
  • In other words, the match works the same way as how the shell matches wildcards on the command line
  • Example: “$str” =~ “cis*”
    • will match if $str starts with cis, followed by 0 or more characters
  • File tests are similar to bash:

-e exist -r read access

-z zero length -w write access

-f regular file -x execute access

-d directory

-l link