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 .bashrc and .profile files
  6. The History Variable
  7. The !! Command
  8. Tab completion
  9. Aliases
  10. Setting a Custom Prompt
  1. What shells are available, and what is the default at LSC/ATM?

    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. In the ATM machines the standard default shell is bash.

  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/bash
    >>
    
    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.

     
        
    [loriotg@gollum ~] finger loriotg
    Login: loriotg        			Name: George B Loriot
    Directory: /mnt/homes/loriotg       	Shell: /bin/bash
    Office: Faculty, 20070118		Home Phone: 99991234^H
    On since Tue Nov  1 13:50 (GMT) on tty7 from :0
        1 hour 31 minutes idle
    On since Tue Nov  1 13:50 (GMT) on pts/0 from :0.0
    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.

     
        
    [loriotg@gollum ~] ps
      PID TTY          TIME CMD
     2502 pts/1    00:00:00 bash
     2827 pts/1    00:00:00 ps
    [loriotg@gollum ~] 
    
    Using the ps command (covered below) will show all processes running in the current shell under your userid at the time the ps command is invoked. In this example it shows that the shell is bash, and the only other process is ps.

  3. Selecting a New Shell

    Select a new shell by entering its name at the command line. For example, to switch to the tcsh shell, enter
    tcsh
    To exit this shell, enter ctl-d or exit as for any shell. In this case you will exit the tcsh shell and return to the bash shell. Selecting a new shell in this way will not chnage your default login shell; the next login will still have bash as your login shell. To change the login shell, use the yppasswd -l command to change your login shell permanently. NOTE: the ATM lab environment setup assumes that the login shell will be bash; changing the login shell will not setup the same environment for the usual ATM applications.

  4. The Shell Environment

    See lecture 2 for a description of some shell environment variables and paths. The next section shows how these are set at login.

  5. The .bashrc and .profile files

    With bash 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.
    .bashrc is read at the startup of each instance of a shell. .profile or .bash_profile is similar to .bashrc but is only read at the creating of the initial shell when the user first logs in.

    These files are shell scripts which contain Linux commands that are executed line by line to set the environment to one which is common to all users. This why all students have the same paths, aliases, and variables. The .bash file is actually a symbolic link to the file /mnt/homes/00links/.bashrc, which is not writeable by the users and therefore all users have the same environment.

    To allow users to change their login environment, the ATM configuration includes a file .user.bashrc in their home directory. This file is always run at the end of normal login and my be edited with, for example, vi to enter environment commands such as export or alias to customize a user's environment.

  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  ls
         2  df
         3  uptime
         4  w
         5  history | tail
         6  history
         7  last | head
         8  env | grep PAGER
         9  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 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 "HISTSIZE". The syntax is shown below:
     
         
    mark@platypus:~>
    mark@platypus:~> export HISTSIZE=5
    mark@platypus:~> history
         9  21:04   history
        10  21:06   history
        11  21:06   uptime
        12  21:06   export HISTSIZE=5
        13  21:06   history
    mark@platypus:~>
    mark@platypus:~> export HISTSIZE=250
    mark@platypus:~>
    

    If you want your shell to remember 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 .user.bashrc file:
     
         
    mark@platypus:~>
    mark@platypus:~> export HISTSIZE=50
    mark@platypus:~>
    

  7. The !! and ! Commands

    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  set history=250
        19  uptime
        20  w
        21  last | head -4
        22  env | grep -i tuckerm
        23  history
        24  man history
        25  man history
        26  history
        27  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:~>
    

    The !string command can also be used to repeat the last command that begins with 'string'. Entering !uptime will invoke the same command that !19 invoked.

  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.
     
         
    [loriotg@apollo unix] alias
    alias d='dir'
    alias dir='/bin/ls $LS_OPTIONS --format=vertical'
    alias fixfirefox='/mnt/homes/00links/scripts/fix_firefox.sh'
    alias ftp='ftp -p'
    alias garpa='/mnt/homes/CLASSES/MET2110/gempak/garpa'
    alias gemshell='/mnt/homes/00links/scripts/run_gempak_shell.csh'
    alias homer='ssh george@homer'
    alias idv='/mnt/homes/00links/scripts/run_idv.sh'
    alias ls='/bin/ls -c -F'
    alias nukeprofile='mv -v /mnt/homes/loriotg/.mozilla /mnt/homes/loriotg/.mozilla-20111101'
    alias pgi?='ps -ef|grep lmgrd'
    alias rm='rm -i'
    alias setenv='echo This is the Bourne shell, use export not setenv'
    alias sfstn='/mnt/homes/00links/gempak/sfstn'
    alias solo='/software/rdss/bin/soloii'
    alias sshx='ssh -X -Y'
    alias troy='ssh george@troy'
    alias v='vdir'
    alias vdir='/bin/ls $LS_OPTIONS --format=long'
    alias vmware='/mnt/homes/00links/scripts/run_vmplayer.sh'
    alias vortex00='ssh vortex00'
    alias weather='/software/weather/bin/linux/weather'
    alias xfortune='fortune|cowsay -W 60'
    [loriotg@apollo unix] 
    

    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 "PS1"
     
    tuckerm@metlab19:~> PS1="BOB# "
    BOB#
    BOB# PS1="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.

    \wcurrent working directory
    %~same as %c
    \uusername
    \hhost
    \Ttime (12 hour format)
    \ttime (24 hour format)
    \@ time (in AM/PM format)
    \!history number

    Here we set the prompt back to the default for the lab:

     
        
    Mark>>
    Mark>> PS1="[\u@\h \W] "
    [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:~> PS1="\! \t>> "
    112 1:41:27>>
    112 1:41:30>>
    112 1:41:34>>