Complete CSH Cheatsheet

Essential commands and features for the C Shell

Quick Reference

C Shell (csh) is a Unix shell with C-like syntax, originally created by Bill Joy at UC Berkeley.

cd
Change directory
cd /usr/local # Change to /usr/local
cd ~ # Change to home directory
cd .. # Move up one directory
cd -
Switch to previous directory
$ cd /var/www
$ cd /etc
$ cd - # Returns to /var/www
dirs
Show directory stack
dirs # Show directory stack
dirs -v # Show with numbers
pushd, popd
Manipulate directory stack
pushd /usr/local # Add to stack and cd
pushd +1 # Rotate stack
popd # Return to previous directory

2 Variables

set
Set local shell variables
set name = "John"
set count = 10
set files = `ls` # Command substitution
setenv
Set environment variables
setenv PATH /usr/local/bin:$PATH
setenv EDITOR vim
setenv TERM xterm-256color
unset, unsetenv
Remove variables
unset name # Remove local variable
unsetenv PATH # Remove environment variable
Special variables
Predefined shell variables
$home # Home directory
$path # Executable search path
$status # Exit status of last command
$$ # Current process ID
$#argv # Number of arguments
Variable modifiers
Manipulate variable values
echo $file:r # Remove extension
echo $file:e # Get extension
echo $path[1] # First element of array
echo $#path # Number of elements

3 Aliases

alias
Create command shortcuts
alias ll 'ls -l'
alias rm 'rm -i'
alias h 'history'
alias # List all aliases
unalias
Remove aliases
unalias ll # Remove ll alias
unalias * # Remove all aliases
Alias arguments
Using arguments in aliases
alias mkcd 'mkdir \!^ && cd \!^'
alias recent 'ls -lt \!* | head'
# \!^ = first argument, \!* = all arguments

4 History

history
View command history
history # Show all commands
history 10 # Show last 10 commands
!! # Repeat last command
!-2 # Repeat command 2 back
Event designators
Reference commands from history
!42 # Execute command #42
!vi # Last command starting with 'vi'
!?config? # Last command containing 'config'
^old^new # Replace in last command
history variables
Configure history behavior
set history = 100 # Remember 100 commands
set savehist = 50 # Save 50 commands between sessions
set histdup = all # Don't save duplicates
history -h
History substitution without execution
history -h !vi # Show last vi command
history -h !! # Show last command
!vi:p # Print without executing

5 Job Control

jobs
List background jobs
jobs # List jobs
jobs -l # List with PIDs
&, bg, fg
Background and foreground jobs
long_command & # Run in background
Ctrl+Z # Suspend current job
bg %1 # Resume job 1 in background
fg %2 # Bring job 2 to foreground
kill
Terminate processes
kill %1 # Kill job 1
kill -9 1234 # Force kill PID 1234
kill -l # List signals
nice, nohup
Process priority and persistence
nice +10 long_command # Lower priority
nohup command & # Run immune to hangups
^Z, ^C, ^D
Control character functions
Ctrl+Z # Suspend process
Ctrl+C # Interrupt process
Ctrl+D # End of file (logout if empty)

6 Scripting

Shebang
CSH script header
#!/bin/csh -f
# -f = fast start (skip .cshrc)
Conditionals
If-then-else statements
if ($user == "root") then
  echo "You are root"
else if ($user == "admin") then
  echo "You are admin"
else
  echo "You are $user"
endif
Loops
For and while loops
# For loop
foreach color (red green blue)
  echo $color
end

# While loop
set count = 1
while ($count <= 5)
  echo "Count: $count"
  @ count++
end
Switch
Case statement
switch ($choice)
  case "start":
    echo "Starting..."
    breaksw
  case "stop":
    echo "Stopping..."
    breaksw
  default:
    echo "Unknown option"
    breaksw
endsw
Arithmetic
Numeric operations
@ sum = 5 + 3
@ count++
@ result = ($x * $y) / $z
echo "Sum is $sum"
Command line args
Handling script arguments
# script.csh arg1 arg2
echo "Script: $0"
echo "First arg: $1"
echo "All args: $argv"
echo "Arg count: $#argv"

7 Keyboard Shortcuts

Editing shortcuts
Command line editing
Ctrl+A # Move to beginning of line
Ctrl+E # Move to end of line
Ctrl+K # Delete to end of line
Ctrl+U # Delete to beginning of line
Ctrl+W # Delete previous word
Completion
File and command completion
Esc, Esc # Complete filename
Ctrl+D # List possible completions
Tab # Complete command (if set)
History navigation
Browse command history
Up/Down # Navigate history
Ctrl+P # Previous command
Ctrl+N # Next command
Ctrl+R # Reverse search
Process control
Manage running processes
Ctrl+C # Interrupt process
Ctrl+Z # Suspend process
Ctrl+D # End of input (logout)
Ctrl+S # Pause output
Ctrl+Q # Resume output