C Shell

{{language |exec=interpreted |tags=csh |hopl id=1936 }}{{implementation|UNIX Shell}} '''csh''' was the shell that William Joy wrote for [[BSD]]. '''csh''' accepted the same [[Unix]] commands as other shells, but had a very different syntax (for variable assignments, control flow, and such). '''csh''' is not compatible with the [[Bourne Shell]].

BSD keeps the C Shell at /bin/csh. [[Hashbang]] lines should use the -f option:

#!/bin/csh -f

== Reputation == C Shell is obsolete. Most scriptwriters prefer a Bourne-compatible shell, and few users want to learn two flavors of shells. C Shell introduced tilde expansion (ls ~), job control, command history, and aliases, but POSIX shells now have all of those.

[http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ Csh Programming Considered Harmful] and [http://www.grymoire.com/Unix/CshTop10.txt Top Ten Reasons not to use the C shell] give multiple reasons to avoid C Shell.

tcsh is a later version that fixed many of the problems with csh. It is still actively, if intermittently, maintained and has a following such as on Solaris.

== Syntax == [http://www.openbsd.org/cgi-bin/man.cgi?query=csh&apropos=0&sektion=1&manpath=OpenBSD+Current&arch=i386&format=html The manual for csh(1)] claims that C Shell has "a C-like syntax". Several other languages have a C-like syntax, including [[Java]] and [[Pike]], and Unix utilities [[AWK]] and [[bc]]. C Shell is less like [[C]] than those other languages.

This example prints a [[Hailstone sequence]] from 13.

{| class="wikitable" ! C ! C Shell |- ||

#include <stdio.h>

int
main()
{
  int n;

  n = 13;
  printf("%d\n", n);
  while (n != 1) {
    if (n % 2)
      n = 3 * n + 1;
    else
      n /= 2;

    printf("%d\n", n);
  }

  return 0;
}

||








@ n = 13
echo $n
while ($n != 1)
  if ($n % 2) then
    @ n = 3 * $n + 1
  else
    @ n /= 2
  endif
  echo $n
end



|}

C Shell has no braces {} to group the commands. Strange keywords are then, endif and end. Expressions have $n instead of n. Assignments use @ n.

C Shell has "a C-like syntax" because C Shell is more like C than [[Bourne Shell]].

{| class="wikitable" ! Bourne Shell ! C Shell |- ||

n=13
echo $n
while test $n -ne 1; do
  if expr $n % 2 >/dev/null; then
    n=`expr 3 \* $n + 1`
  else
    n=`expr $n / 2`
  fi
  echo $n
done

||

@ n = 13
echo $n
while ($n != 1)
  if ($n % 2) then
    @ n = 3 * $n + 1
  else
    @ n /= 2
  endif
  echo $n
end

|}

Bourne Shell requires test or expr to evaluate expressions. C Shell has built-in expressions, so the Hailstone sequence comes more easily. These expressions have a stupid quirk: all operators are right-associative, so 10 - 3 - 2 acts like 10 - (3 - 2). The fix is to use parentheses.

% @ n = 10 - 3 - 2
% echo $n
9
% @ n = (10 - 3) - 2
% echo $n
5

== Links ==

  • [[OpenBSD]] has [http://www.openbsd.org/cgi-bin/man.cgi?query=csh&apropos=0&sektion=1&manpath=OpenBSD+Current&arch=i386&format=html csh(1) manual] and [http://www.openbsd.org/cgi-bin/cvsweb/src/bin/csh/ source code].

Tasks