Notes: Unix Lab 12

Command Shells

  1. What shells are available, how do they differ from each other?
  2. How to Identify the Shell You Are Using
  3. Selecting a New Shell
  4. The Shell Environment
  5. The .cshrc and .login files
  6. The History Variable
  7. The !! Command
  8. Tab completion
  9. Aliases
  10. Setting a Custom Prompt
  1. What shells are available, how do they differ from each other?

    There are many shells available in the unix environment. Below some of the more common shells are listed:
    • sh
    • bash
    • ksh
    • tcsh
    • csh
    • zsh
    The most frequently used shells are sh (Bourne shell), csh (C-shell) and ksh (Korn shell). bash is an enhanced version of the Bourne shell and tcsh is an enhanced version of csh. For most common usage the differences between the different shells is minimal. Most of the differences are with the numerous "enhancements" that various shells provide such as tab completion, job control, shell features (such as aliases) and scripting syntax.

  2. How to Identify the Shell You Are Using

    There are several methods for determining what unix shell you are currently using:

     
        
    >>
    >> echo $SHELL
    /bin/tcsh
    >>
    
    The environment variable $SHELL will not always be available and is not present with some shells or versions of unix. It is also unreliable as it will not always identify the shell correctly if different shell is started from the login shell.

     
        
    >>
    >> finger tuckerm
    Login: tuckerm                          Name: Mark Tucker
    Directory: /mnt/homes/tuckerm           Shell: /bin/tcsh
    Office: Staff, 20030601
    On since Tue Mar  2 17:44 (GMT) on :0 (messages off)
    Mail forwarded to mark.tucker@lyndonstate.edu
    No mail.
    No Plan.
    >>
    
    Using the finger command for the current login name will show the default login shell for that user. This will not show what is currently being used if the user has started a different shell.

     
        
    >>
    >> grep tuckerm /etc/passwd
    tuckerm:x:3005:3005:Mark Tucker,Staff,20030601,:/mnt/homes/tuckerm:/bin/tcsh
    >>
    
    Using grep to filter on the unix username from the file /etc/passwd will likewise show the default login shell. Entries in /etc/passwd are delimited with the ":" character. The path to the user's default login shell will be the last element of the line.

     
        
    >>
    >> ps -ef |grep $$
    tuckerm  19000 18951  0 17:50 pts/1    00:00:00 -tcsh
    tuckerm  19040 19000  0 18:07 pts/1    00:00:00 ps -ef
    tuckerm  19041 19000  0 18:07 pts/1    00:00:00 grep 19000
    >>
    >> ps aux |grep $$
    tuckerm  19000  0.0  0.1  2928 1604 pts/1    S    17:50   0:00 -tcsh
    >>
    
    The above example shows two methods of finding out what the default shell is for csh/tcsh and Bourne shell variants such as /bin/bash. The $$ variable will return the current process id (which is the interactive shell in this instance). The ps command lists all processes on the system and grep filters the current shell's process ID to show the name of the executable which is running with the PID (process ID), tcsh in this example.. The above example shows the SysV and BSD syntax for ps (respectively). This is the most reliable method for determining the current shell in use.

  3. Selecting a New Shell

    There are two basic methods for selecting a new shell for interactive use:
    1. execute the new shell by entering it in the default shell in a terminal
    2. Use the command chsh. This command will alter the login shell in /etc/passwd permanently. NOTE: Do not do this with your account in the lab since many of the settings for the meteorology applications are made with the assumption that you will be using the default login shell (tcsh) already configured for your account. 3. use the command "passwd -s" to set the login shell. This is the equivalent of using the chsh command and has the same implications.

  4. The Shell Environment

    See lecture 2.

  5. The .cshrc and .login files

    With csh/tcsh there are two files which alter the shell's behavior and the user environment. These allow you to customize your shell environment for interactive use and to set environment variables for various applications.
    .cshrc is read at the startup of each instance of a shell. .login is similar to .cshrc but is only read at the creating of the initial shell when the user first logs in.

  6. The History Variable

    Unix shells generally have the ability to keep a history of the commands you type in a given shell. It is unique to each instance of the shell. The command history can be used to show a listing of the commands that have been typed into the shell.
     
        
    mark@platypus:~>
    mark@platypus:~> history
         1  21:03   ls
         2  21:03   df
         3  21:03   uptime
         4  21:03   w
         5  21:03   history | tail
         6  21:03   history
         7  21:03   last | head
         8  21:04   env | grep PAGER
         9  21:04   history
    mark@platypus:~>
    
    Note that the output of the history command has a number for each command in the shell's history. The second column is the time that the command was entered. Following the time column is the actual command that was entered to the shell.

    The number of commands saved in history can be limited (or expanded) by setting the value of the shell variable "history". The syntax is shown below:
     
         
    mark@platypus:~>
    mark@platypus:~> set history=5
    mark@platypus:~> history
         9  21:04   history
        10  21:06   history
        11  21:06   uptime
        12  21:06   set history=5
        13  21:06   history
    mark@platypus:~>
    mark@platypus:~> set history=250
    mark@platypus:~>
    

    If you want your shell to remeber your commands after you logout so that the next time you login they will appear in your history list, add the following command to your .cshrc or .tcshrc file:
     
         
    mark@platypus:~>
    mark@platypus:~> set savehist=50
    mark@platypus:~>
    

  7. The !! Command

    The shell's command history is not of much use without the ability to repeat these commands. On many systems the up arrow can be used to bring the previous commands back to the current prompt where they may be entered again. Similarly, the !! command will repeat the last command entered.
     
         
    mark@platypus:~>
    mark@platypus:~> man history
    mark@platypus:~> !!
    man history
    mark@platypus:~>
    

    Specific items from the command history may be repeated by using the exclaimation point followed by the history number of the command. (ex. !30 would repeat the command number 30 from the shell's history.)
     
         
    mark@platypus:~>
    mark@platypus:~> history |tail -10
        18  21:07   set history=250
        19  21:07   uptime
        20  21:07   w
        21  21:07   last | head -4
        22  21:08   env | grep -i tuckerm
        23  21:08   history
        24  21:10   man history
        25  21:13   man history
        26  21:14   history
        27  21:14   history | tail -10
    mark@platypus:~> !19
    uptime
     21:14:37 up 34 days,  3:46,  1 user,  load average: 0.08, 0.06, 0.01
    mark@platypus:~>
    

  8. Tab completion

    This functionality is avialable with more recently developed shells such as tcsh and bash. Will try to complete current text on the command line with a match using a relative or absolute path.

  9. Aliases

    An alias is a way of creating a new or customized command for use in the unix shell. When typed into the shell, the alias then runs the command that it is defined to run. As an example, if the command "ls -Fa" were aliased as "zoom" then every time the command "zoom" was entered it would run "ls -Fa".

    The alias command entered without any arguments will show what commands are aliased in the current shell. Syntax for the alias is different for Bourne shell compared with C shells.
     
         
    tuckerm@apollo:~>
    tuckerm@apollo:~> alias
    d       dir
    dir     /usr/bin/ls $LS_OPTIONS --format=vertical
    ls      /usr/bin/ls $LS_OPTIONS
    rm      rm -i
    solo    /software/rdss/bin/soloii
    v       vdir
    vdir    /usr/bin/ls $LS_OPTIONS --format=long
    weather /software/weather/bin/weather
    tuckerm@apollo:~>
    

    Below is an example of creating an alias "lls" which would run the command "ls -l" when entered.
     
         
    tuckerm@apollo:~>
    tuckerm@apollo:~> alias lls "ls -l"
    tuckerm@apollo:~> lls wrf.tgz
    -rw-r--r--    1 tuckerm  tuckerm  13601558 Feb 18 18:18 wrf.tgz
    tuckerm@apollo:~>
    

    An alias may be removed from the shell with the command unalias.
     
         
    tuckerm@apollo:~>
    tuckerm@apollo:~> unalias lls
    tuckerm@apollo:~> lls
    lls: Command not found.
    tuckerm@apollo:~>
    

  10. Setting a Custom Prompt

    The prompt is the text at the beginning of the shell's command line. In our lab configuration it is set to display the username, hostname and current directory as shown below:
     
    tuckerm@metlab19:~>
    tuckerm@metlab19:~>
    
    In this case, the user logged in is "tuckerm" on the host metlab19 and the current working directory is the user's home (~).

    The prompt can be changed by setting the shell variable "prompt"
     
    tuckerm@metlab19:~> set prompt="BOB# "
    BOB#
    BOB# set prompt="Mark>>"
    Mark>>
    Mark>>
    
    In the example above, the prompt was set to the string "BOB" and then to "Mark>>" Almost any text can be used. In addition, there are several special strings that can create a more useful prompt. Examples are found further down the page.

    %Ccurrent working directory
    %ccurrent working directory only using "~" for the home directory
    %~same as %c
    %nusername
    %mhost
    %ttime
    %Ttime (24 hour format)
    %P time (24 hour format with seconds)
    %hhistory
    %UTEXT%uTEXT will be underlined
    %BTEXT%bTEXT will be bolded
    %STEXT%sTEXT will be in reverse text

    Here we set the prompt back to the default for the lab:
     
        
    Mark>>
    Mark>> set prompt = "%n@%m:%c> "
    tuckerm@metlab19:~>
    tuckerm@metlab19:~>
    

    Below the prompt is set to show the command history number and current time in 24 hour format:
     
        
    tuckerm@metlab19:~>
    tuckerm@metlab19:~> set prompt="%h %P>> "
    112 1:41:27>>
    112 1:41:30>>
    112 1:41:34>>
    

    Set the prompt to use the underlined string "Linux"
     
           
    tuckerm@metlab19:~>
    tuckerm@metlab19:~> set prompt="%ULinux%u> "
    Linux>
    Linux>