Category:C Shell: Difference between revisions
(Copy text from C Shell. Wiki prevents move to 'Category:' namespace. Authors: Kernigh and Dkf. http://rosettacode.org/mw/index.php?title=C_Shell&action=history) |
(Fill 'Syntax' and 'Reputation' sections.) |
||
Line 1: | Line 1: | ||
{{language |
{{language |
||
|exec=interpreted |
|exec=interpreted |
||
|tags=csh}}{{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]]. |
'''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 |
BSD keeps the C Shell at <code>/bin/csh</code>. [[Hashbang]] lines should use the -f option: |
||
[[Hashbang]] lines for C shell scripts should use the -f option: |
|||
<lang csh>#!/bin/csh -f</lang> |
<lang csh>#!/bin/csh -f</lang> |
||
== 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 (<code>ls ~</code>), history recall, job control and aliases, but POSIX shells now have all of those. |
|||
⚫ | |||
== Syntax == |
== 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. |
|||
{| class="wikitable" style="clear: right; width: 100%" |
|||
This example prints a [[Hailstone sequence]] from 13. |
|||
{| class="wikitable" |
|||
! C |
! C |
||
! C Shell |
! C Shell |
||
⚫ | |||
|- |
|- |
||
|| <lang c>#include <stdio.h> |
|| <lang c>#include <stdio.h> |
||
Line 55: | Line 62: | ||
</lang> |
</lang> |
||
|} |
|||
⚫ | |||
C Shell has no braces {} to group the commands. Strange keywords are <code>then</code>, <code>endif</code> and <code>end</code>. Expressions have <code>$n</code> instead of <code>n</code>. Assignments use <code>@ n</code>. |
|||
C Shell has "a C-like syntax" because C Shell is more like C than [[Bourne Shell]]. |
|||
{| class="wikitable" |
|||
⚫ | |||
n=13 |
|||
! C Shell |
|||
|- |
|||
⚫ | |||
echo $n |
echo $n |
||
while test $n -ne 1; do |
while test $n -ne 1; do |
||
Line 71: | Line 81: | ||
fi |
fi |
||
echo $n |
echo $n |
||
done |
done</lang> |
||
⚫ | |||
echo $n |
|||
while ($n != 1) |
|||
⚫ | |||
if ($n % 2) then |
|||
@ n = 3 * $n + 1 |
|||
else |
|||
@ n /= 2 |
|||
endif |
|||
echo $n |
|||
⚫ | |||
|} |
|} |
||
Bourne Shell requires <code>test</code> or <code>expr</code> 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 <code>10 - 3 - 2</code> acts like <code>10 - (3 - 2)</code>. The fix is to use parentheses. |
|||
<lang csh>% @ n = 10 - 3 - 2 |
<lang csh>% @ n = 10 - 3 - 2 |
||
% echo $n |
% echo $n |
||
9 |
|||
9</lang> |
|||
% @ n = (10 - 3) - 2 |
|||
⚫ | |||
% echo $n |
% echo $n |
||
5</lang> |
5</lang> |
||
== Links == |
== Links == |
||
* [http://www.openbsd.org/cgi-bin/man.cgi?query=csh&apropos=0&sektion=1&manpath=OpenBSD+Current&arch=i386&format=html csh(1) manual] |
* [[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]. |
||
⚫ | |||
* [http://www.grymoire.com/Unix/CshTop10.txt Top Ten Reasons not to use the C shell] |
Revision as of 20:29, 9 August 2011
This programming language may be used to instruct a computer to perform a task.
Execution method: | Interpreted |
---|---|
Lang tag(s): | csh |
See Also: |
|
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:
<lang csh>#!/bin/csh -f</lang>
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 ~
), history recall, job control and aliases, but POSIX shells now have all of those.
Csh Programming Considered Harmful and Top Ten Reasons not to use the C shell give multiple reasons to avoid C Shell.
Syntax
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.
C | C Shell |
---|---|
<lang c>#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; }</lang> |
<lang csh>
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.
Bourne Shell | C Shell |
---|---|
<lang bash>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</lang> |
<lang csh>@ n = 13
echo $n while ($n != 1) if ($n % 2) then @ n = 3 * $n + 1 else @ n /= 2 endif echo $n end</lang> |
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.
<lang csh>% @ n = 10 - 3 - 2 % echo $n 9 % @ n = (10 - 3) - 2 % echo $n 5</lang>
Links
- OpenBSD has csh(1) manual and source code.
Subcategories
This category has the following 3 subcategories, out of 3 total.
@
- C Shell examples needing attention (empty)
- C Shell Implementations (empty)
- C Shell User (4 P)
Pages in category "C Shell"
The following 31 pages are in this category, out of 31 total.