|   | 
  | 
 
  | 
 | 
 | 
 
 | 
|   | 
  | 
 
  | 
|   | 
  | 
Features (csh)
 | 
 
 | 
 | 
Each shell has its own set of features. Those of the C Shell are discussed below.  | 
|   | 
  | 
 
  | 
|   | 
  | 
Command history
 | 
 
 | 
 | 
The history mechanism maintains a list of recently used command lines, called events. Provides a shorthand for reexecuting previous commands. To use history:  | 
|   | 
  | 
 
  | 
 | 
 | 
1. Set the history variable:  | 
 | 
 | 
     set history = 100  | 
|   | 
  | 
 
  | 
 | 
 | 
2. Issue the history command and view the output:  | 
|   | 
  | 
 
  | 
 | 
 | 
 %history
 35  12:34   sort < unsorted > sorted.list
 36  12:34   cat sorted.list
 37  12:35   ls * > unsorted
 38  12:35   cat unsorted
 39  12:35   ls
 40  13:06   history
 41  13:08   alias
 42  13:11   ls
 43  13:11   vi .cshrc
 
 | 
|   | 
  | 
 
  | 
 | 
 | 
3. To save history events across all login sessions, set the savehistory variable:  | 
 | 
 | 
   set savehistory = 50  | 
|   | 
  | 
 
  | 
|   | 
  | 
Event reexecution
 | 
 
 | 
 | 
Allows you to specify a shorthand for reexecuting a previous event. Works with the history list.  | 
|   | 
  | 
 
  | 
 | 
 | 
     !!          - repeats last command   | 
 | 
 | 
     !number     - repeats numbered command from history list  | 
 | 
 | 
     !string     - repeats last command starting with string   | 
|   | 
  | 
 
  | 
|   | 
  | 
Modifying previous events
 | 
 
 | 
 | 
Allows you to correct typos in previous command, or modify it to create a new command.  | 
 | 
 | 
      ^old^new            - changes the string "old" to the string "new" in the last command  issued   | 
 | 
 | 
     !number:s/old/new/   - changes numbered command from history list; substitutes the string "old" with the string "new".  Note that there is no space between number and :   | 
|   | 
  | 
 
  | 
|   | 
  | 
Aliases
 | 
 
 | 
 | 
The alias command allows you to define new commands. Useful for creating shorthands for longer commands. The syntax is.  | 
|   | 
  | 
 
  | 
 | 
 | 
     alias  entered_command  executed_command  | 
|   | 
  | 
 
  | 
 | 
 | 
Some examples:  | 
|   | 
  | 
 
  | 
 | 
 | 
     alias  m     more  | 
 | 
 | 
     alias  rm    "rm -i"  | 
 | 
 | 
     alias  h     "history -r | more"  | 
 | 
 | 
     alias  www  "cd /home/deepak/asic-world"  | 
|   | 
  | 
 
  | 
 | 
 | 
To view all current aliases:  | 
|   | 
  | 
 
  | 
 | 
 | 
     alias   | 
|   | 
  | 
 
  | 
 | 
 | 
To remove a previously defined alias:  | 
|   | 
  | 
 
  | 
 | 
 | 
     unalias alias_name  | 
|   | 
  | 
 
  | 
 | 
 | 
 
 | 
|   | 
  | 
 
  | 
|   | 
  | 
Filename Generation
 | 
 
 | 
 | 
When you give the shell abbreviated filenames which contain special characters (metacharacters), it can generate filenames which match existing files. Some examples appear below:  | 
|   | 
  | 
 
  | 
 | 
 | 
| 
      Command       
 | 
  Description 
 | 
 
| 
      ls  *.txt     
 | 
  list files with .txt suffix  
 | 
 
| 
      ls [abc]*     
 | 
  list files with names that start with a, b or c 
 | 
 
| 
      lpr prog?.c   
 | 
  print files named prog?.c where ? is any character 
 | 
 
| 
      cd ~jsmith    
 | 
  change to user jsmith's home directory 
 | 
 
 
 | 
 | 
 | 
You can "turn off" filename generation by setting the noglob variable. This will permit special characters to be interpreted literally. For example:  | 
|   | 
  | 
 
  | 
 | 
 | 
     set noglob   | 
|   | 
  | 
 
  | 
|   | 
  | 
Filename Completion
 | 
 
 | 
 | 
The shell will complete a filename for you if you type in only enough characters to make the name unique. This feature is a great time saver and helps to prevent typos when trying to type long filenames.  | 
|   | 
  | 
 
  | 
 | 
 | 
To use filename completion, you need to set filec, either on the command line or in one of your initialization files.  | 
|   | 
  | 
 
  | 
 | 
 | 
     set filec   | 
|   | 
  | 
 
  | 
 | 
 | 
Then, when specifying a filename, type the part which is unique and hit the escape key (C shell) or tab key (TC Shell). For example, if you had a directory with a long name, such as "Introduction.UNIX.Filesystems", you could cd to that directory by using the cd command with only a portion of the file's name, provided that the portion you specify is unique (no other files have similar names)  | 
|   | 
  | 
 
  | 
 | 
 | 
    cd Intro<ESC>  | 
|   | 
  | 
 
  | 
 | 
 | 
Note: typing a portion of a filename and then hitting CTRL-D instead of ESCape or TAB will display a list of the filenames which match.  | 
|   | 
  | 
 
  | 
|   | 
  | 
Variables (csh)
 | 
 
 | 
 | 
Each shell has its own set of variables and rules for using variables. Those associated with the C Shell are discussed below.  | 
 | 
 | 
-     The shell has variables which are predefined as well as variables which you define (user defined).
 
-     Shell variables control many aspects of how your shell environment behaves. Modifying these variables (and creating new ones) allows you to customize your shell environment.
 
-     Shell variables are used extensively when creating shell scripts (covered later).
 
 
 | 
|   | 
  | 
 
  | 
 | 
 | 
    Variables can be  | 
 | 
 | 
-           local - current shell only
 
-           global - current shell and child processes/shells
 
-           string - treated as character
 
-           numeric - treated as numbers
 
-           arrays - contain more than one value 
 
 
 | 
 | 
 | 
    Commands used to declare and manipulate shell variables:  | 
|   | 
  | 
 
  | 
 | 
 | 
| 
      Command         
 | 
  Description 
 | 
 
| 
      set             
 | 
  assigns non-numeric string variables locally 
 | 
 
| 
      unset           
 | 
  removes a previously "set" variable  
 | 
 
| 
      set             
 | 
  shows all "set" variables 
 | 
 
| 
      setenv          
 | 
  assigns non-numeric string variables globally 
 | 
 
| 
      unsetenv        
 | 
  removes a previously setenv variable 
 | 
 
| 
      setenv          
 | 
  shows all setenv variables 
 | 
 
| 
      @               
 | 
  assigns numeric variables locally 
 | 
 
| 
      echo $variable  
 | 
  displays value of variable 
 | 
 
 
 | 
|   | 
  | 
 
  | 
 | 
 | 
Examples  | 
|   | 
  | 
 
  | 
 | 
 | 
| 
      Command                      
 | 
  Description 
 | 
 
| 
      set name=Deepak                
 | 
  Sets variable name to value of Deepak 
 | 
 
| 
      unset name                   
 | 
  Unsets the variable name 
 | 
 
| 
      set path=($path . ~/bin)     
 | 
  Adds to current setting of string array variable path 
 | 
 
| 
      echo $HOME                   
 | 
  Displays value of variable HOME 
 | 
 
| 
      set colors=(red green blue)  
 | 
  Assigns three values to the string array variable colors 
 | 
 
| 
      @ count = 1                  
 | 
  Sets numeric variable count to one 
 | 
 
| 
      @ count = ($count + 1)       
 | 
  Adds one to numeric variable count 
 | 
 
| 
      set counts = (1 22 4 9)      
 | 
  Assigns 4 values to numeric array variable counts 
 | 
 
| 
      @ counts[2] = 5              
 | 
  Assigns values to second element of numeric array variable counts 
 | 
 
| 
      @ echo $counts[3]            
 | 
  Displays value of third element of numeric array variable counts 
 | 
 
 
 | 
|   | 
  | 
 
  | 
|   | 
  | 
 
  | 
|   | 
  | 
Predefined Shell Variables. 
 | 
 
 | 
 | 
A number of shell variables are predefined by the shell itself or inherited from the system environment. Some of the more common ones are described below.  | 
|   | 
  | 
 
  | 
 | 
 | 
| 
       Variable 
 | 
  Description 
 | 
 
| 
       $             
 | 
   Contains the process id of the current shell 
 | 
 
| 
       argv          
 | 
    Contains the command line arguments for an invoked command. argv is an array, with $argv[0] set to the name of the invoked command, $argv[1] set to the first argument, $argv[2] set to the second argument...and so on. $argv[*] can be used to specify all arguments. You may also use the shorthand $n where n is the number of the argument. 
 | 
 
| 
       #argv         
 | 
   Set to the actual number of arguments in argv, excluding argv[0] 
 | 
 
| 
       cdpath        
 | 
  Expands the search path for the cd command. By default, the cd command issued with a simple filename will search only the working directory. Use cdpath to increase the number of directories searched. 
 | 
 
| 
       CWD or cwd    
 | 
  Holds that name of the working/current directory. 
 | 
 
| 
       echo          
 | 
   Causes the shell to echo the command before executing it. Use set/unset to turn this on/off. 
 | 
 
| 
       filec         
 | 
  Enables file completion. Use set/unset to turn this on/off. 
 | 
 
| 
       history       
 | 
   Controls the size of the history list. 100 is recommended as safe size. If the number is too large, the shell may run out of memory. 
 | 
 
| 
       HOME or home  
 | 
    The pathname of your home directory 
 | 
 
| 
       ignoreeof     
 | 
   Prevents exiting the shell by typing CTRL-D, and thus, prevents accidentally logging off. Use set/unset to turn this on/off. 
 | 
 
| 
       noclobber     
 | 
  Prevents you from accidentally overwriting a file when you redirect output. Use set/unset to turn this on/off. 
 | 
 
| 
       noglob        
 | 
   Prevents the shell from generating/expanding ambiguous filenames. Use set/unset to turn this on/off. 
 | 
 
| 
       notify        
 | 
   Tells the shell to notify you immediately when a background job completes. Ordinarily, notification will wait until the next shell prompt to prevent interruption of work. Use set/unset to turn this on/off. 
 | 
 
| 
       PATH or path  
 | 
  Specifies the path that the shell searches when asked to execute a command. If an executable is not found in the path, you must specify its full pathname. 
 | 
 
| 
       prompt        
 | 
  Allows you to customize the shell prompt. By default, the C shell prompt is simply a percent sign (%). For example, to display the machine name you are logged into as part of your prompt set prompt =  "`hostname -s`% "  
 | 
 
| 
       savehist      
 | 
  Specifies how many the number of command events to save as history after you logout. These events are saved in a file called .history in your home directory. The shell uses these as your initial history after you login again. 
 | 
 
| 
       shell         
 | 
  Contains the pathname of the shell 
 | 
 
| 
       status        
 | 
  Contains the exit status of the last executed command 
 | 
 
| 
       USER or user  
 | 
   Contains you login userid 
 | 
 
| 
       verbose       
 | 
   Causes the shell to display each command after a history substitution. Use set/unset to turn this on/off.  
 | 
 
 
 | 
|   | 
  | 
 
  | 
|   | 
  | 
Initialization Files
 | 
 
 | 
 | 
System-wide shell initialization files are common on UNIX systems. These files can be "sourced" automatically by the shell and are typically setup by the System Administrator for the local environment.  | 
|   | 
  | 
 
  | 
 | 
 | 
Some examples of system-wide initialization files might be:  | 
|   | 
  | 
 
  | 
 | 
 | 
     /etc/environment  | 
 | 
 | 
     /etc/profile  | 
 | 
 | 
     /etc/cshrc  | 
 | 
 | 
     /etc/login  | 
|   | 
  | 
 
  | 
 | 
 | 
Every shell provides a means for the user to customize certain aspects of the shell's behavior. These customizations usually permit you to augment and/or override the system-wide defaults. User customizations are specified in initialization files located in the top level of your home directory. Naming: Depending upon the shell, you must name your initialization file(s) accordingly.  | 
|   | 
  | 
 
  | 
 | 
 | 
Executed during interactive login   | 
|   | 
  | 
 
  | 
 | 
 | 
| 
       .login         
 | 
  csh, tcsh 
 | 
 
| 
       .profile       
 | 
  sh, ksh, bash 
 | 
 
| 
       .bash_profile  
 | 
  bash (alternative 1) 
 | 
 
| 
       .bash_login    
 | 
  bash (alternative 2) 
 | 
 
 
 | 
|   | 
  | 
 
  | 
 | 
 | 
Executed for every new shell   | 
 | 
 | 
| 
       .cshrc         
 | 
  csh, tcsh 
 | 
 
| 
       .tcshrc        
 | 
  tcsh 
 | 
 
| 
       .kshrc         
 | 
  ksh 
 | 
 
| 
       .bashrc        
 | 
  bash 
 | 
 
 
 | 
|   | 
  | 
 
  | 
 | 
 | 
The C shell uses two files to set user preferences:  | 
|   | 
  | 
 
  | 
 | 
 | 
    * .cshrc  | 
 | 
 | 
-           runs at each login before the .login file
 
-           runs whenever another shell is created
 
-           runs when a new window is opened
 
-           runs when many utilities are invoked
 
-           typically sets alias information, path variable, filec, prompt, etc. 
 
 
 | 
|   | 
  | 
 
  | 
 | 
 | 
   * .login  | 
 | 
 | 
-           runs at invocation of login shell, after the .cshrc file
 
-           typically sets terminal characteristics and one time shell options and environment variables 
 
 
 | 
|   | 
  | 
 
  | 
 | 
 | 
After changing your .login or .cshrc files, you must "source" them for the changes to take effect:  | 
|   | 
  | 
 
  | 
 | 
 | 
     source .login  | 
 | 
 | 
     source .cshrc  | 
|   | 
  | 
 
  | 
 | 
 | 
The system administrator may/may not provide you with default .cshrc and .login files when you first obtain your userid. If they are provided, be careful about modifying them - especially removing specifications which are required for your local system.  | 
|   | 
  | 
 
  | 
 | 
 | 
Example .cshrc and .login files are provided below:  | 
|   | 
  | 
 
  | 
 | 
 | 
    * .cshrc file  | 
 | 
 | 
    * .login file   | 
|   | 
  | 
 
  | 
|   | 
  | 
Logout Files
 | 
 
 | 
 | 
You are able to specify commands which the shell will execute upon logout. These commands are kept in a file located in the top level of your home directory.  | 
|   | 
  | 
 
  | 
 | 
 | 
     .logout         - csh, tcsh   | 
 | 
 | 
     .bash_logout    - bash   | 
|   | 
  | 
 
  | 
|   | 
  | 
 
  | 
|   | 
  | 
 
  | 
 | 
 | 
 
 | 
|   | 
  | 
 
  |