Alternate Shells



  1. Bash Shell.

    The Bourne Again Shell. This shell is based on the bourne shell and all of the features and syntax of the Bourne shell work in the BASH shell.

    The BASH shell is the standard shell for Linux. Actually when you specify the Bourne shell, Linux actually uses the BASH shell.

  2. Korn Shell.

    The Korn Shell. The Korn shell, designed at Bell Labs by David Korn, is upward compatible with the Bourne shell. Scripts written for the Bourne should run without modification under the Korn Shell. The Korn Shell provides new capabilities for shell scripts.



    1. History

      A history mechanism with a built-in editor lets you edit and reissue commands. You can also correct your error before you hit return. In order to be in edit mode you must set the -o option like:

      set -o vi
      
      Then when you type commands you can edit them by hitting the escape key.
      $ echo hello therr           ( hit esc which put the cursor on r)
      $ echo hello there           ( hit r to replace and type e )
      $ echo hello there
      hello there                  ( the hit reurn and the command is executed)
      
      You can edit the recent history by using the j and k keys.
      $ history
      82      history
      83      man ksh
      84      man ksh >ksh
      85      enscript -G ksh
      86      lpq
      87      ls
      88      alias
      89      fc -l
      90      exit
      91      set -o vi
      92      echo hello there
      93      history
      94      history
      95      ls -al
      96      ls -alg
      97      history
      $ history              ( hit esc the last command appears)
      $ ls -alg              ( k to move up in history file    )
      $ ls -al               ( k to move up in history file    )
      $ ls -a                ( delete the l and return         )
      .               cmkk126.c       grepdata        lab             try1
      ..              core            h.raw           letter.c        try2
      a.out           data            h2.dat          newdata         x4key
      address         f2c.c           junk            nformat.c
      case            format.c        k2.dat          testl.c
      $
      
      The r command just reissues the last command.
      $ r
      ls -a
      .               cmkk126.c       grepdata        lab             try1
      ..              core            h.raw           letter.c        try2
      a.out           data            h2.dat          newdata         x4key
      address         f2c.c           junk            nformat.c
      case            format.c        k2.dat          testl.c
      $
      


    2. Arithmetic

      The Korn Shell allows built-in integer arithmetic via the let command.

      $ i=3
      $ let i=1+1
      $ echo $i
      2
      $ let i=i*3
      $ echo $i
      6
      


    3. Pattern matching in File names

      The Korn shell allows pattern list for file name expansion. Patterns are separated by |.

      $ ls
      a135.t  awk2    awk5    awk8    awkb    data1   dup     junk    rmz
      adata   awk3    awk6    awk9    back    data2   go      level   wnames
      awk1    awk4    awk7    awka    data    dater   guess   ow
      $ l
      
      ?(pattern-list) matches any one of the given patterns.
      $ ls ?(a*|*a)
      a135.t  awk1    awk3    awk5    awk7    awk9    awkb
      adata   awk2    awk4    awk6    awk8    awka    data
      $
      


    4. Aliases

      An alias feature like the C Shell has been added to the Korn Shell.

      $ alias ll='ls -F'
      $ ll
      q1      q1mu    q2.ans  q3      q4      q4.bak
      q1ans   q2      q2.t    q3.ans  q4.ans
      $
      


    5. Arrays

      The Korn shell allows limited arrays and integer type variables.

      $ cat ks1
      integer k=1
      for i
      do
       x[$k]=$i
       let k=k+1
      done
      let k=k-1
      while [ k -gt 0 ]
      do
       echo ${x[k]}
       let k=k-1
      done
      $
      $ ks1 a b c
      c
      b
      a
      $
      


  3. C Shell

    The C Shell. The C shell was developed at UCB by Bill Joy.



    1. set

      Variables are assigned with the set command.

      % set now=then
      % echo $now
      then
      


    2. Aliases

      The C shell accepts aliases like the Korn shell.

      % alias dir ls -FC
      % dir
      ks1*    q1ans   q2      q2.t    q3.ans  q4.ans
      q1      q1mu    q2.ans  q3      q4      q4.bak
      


    3. History

      The C shell keeps a command history, that can be edited.

      % history
          66  vnews -n all
          67  ls
          68  cd outline/unix
          69  histrory
          70  history
          71  ls -al
          72  ls
          73  ls -FC
          74  ls -a
          75  history
      

      I can reissue the last command with !!.

      % ls
      a.out           a060            a120            a160            core
      a010            a070            a130            a170            echo.c
      a020            a080            a135            a180            eqn
      a030            a090            a135.t          a190            makefile
      a040            a100            a140            assignments     outline
      a050            a110            a150            class           quizes
      % !!
      ls
      a.out           a060            a120            a160            core
      a010            a070            a130            a170            echo.c
      a020            a080            a135            a180            eqn
      a030            a090            a135.t          a190            makefile
      a040            a100            a140            assignments     outline
      a050            a110            a150            class           quizes
      

      Or I can recall a command by number or by a match.

      % !73
      ls -FC
      a.out*          a060            a120            a160            core
      a010            a070            a130            a170            echo.c
      a020            a080            a135            a180            eqn
      a030            a090            a135.t          a190            makefile
      a040            a100            a140            assignments/    outline
      a050            a110            a150            class/          quizes/
      % !cd
      cd outline/unix
      ~/outline/unix
      

      Commands may be edited when I reissue them

      !cd:s/unix/c/
      cd outline/c
      ~/outline/c
      


    4. pushd and popd

      Two nice commands of the C shell are pushd and popd. pushd does a cd but remembers the last directory so we can popd back to it.

      % pwd
      /home/kochis/outline/unix
      % pushd ~/c/utility
      ~/c/utility ~/outline/unix
      
      % pushd ~/outline/c/tests
      ~/outline/c/tests ~/c/utility ~/outline/unix
      
      % popd
      ~/c/utility ~/outline/unix
      
      % popd
      ~/outline/unix
      


    5. Arithmetic

      The C shell allows for integer arithmetic.

      % set k=1
      % @ k = $k + 2
      % echo $k
      3
      


    6. Selection

      Decision making can be done with the if or switch statements.



      1. if

        The if statement is :

          if ( expression) 
             command
          else if (expression) 
             command
          else
             command
          endif
        
        
        #!/bin/csh
        set month=`date "+%m"`
        echo the month number is $month
        if ("$month" == 01) then
        	echo January
        else if ("$month" == 02) then
        	echo February
        else if ("$month" == 03) then
        	echo March
        else if ("$month" == 04) then
        	echo April
        else if ("$month" == 05) then
        	echo May
        else if ("$month" == 06) then
        	echo June
        else if ("$month" == 07) then
        	echo July
        else if ("$month" == 08) then
        	echo August
        else if ("$month" == 09) then
        	echo September
        else if ("$month" == 10) then
        	echo October
        else if ("$month" == 11) then
        	echo November
        else 
        	echo December
        endif
        


      2. switch

        The switch statement is:

          switch (string)
            case pattern1 :
                    command
                    breaksw
            case patternn :
                    command
                    breaksw
            default :
                    command
                    breaksw
            endsw
        
        #!/bin/csh
        set month=$1
        switch ("$month")
        	case   1 : 
        		echo January 
        		breaksw
        	case   2 : 
        		echo February 
        		breaksw
        	case   3 : 
        		echo March  
        		breaksw
        	case   4 : 
        		echo April 
        		breaksw
        	case   5 : 
        		echo May  
        		breaksw
        	case   6 : 
        		echo June 
        		breaksw
        	case   7 : 
        		echo July 
        		breaksw
        	case   8 : 
        		echo August  
        		breaksw
        	case   9 : 
        		echo September 
        		breaksw
        	case   10: 
        		echo October  
        		breaksw
        	case   11: 
        		echo November 
        		breaksw
        	case   12: 
        		echo December 
        		breaksw
        	default  :
        		echo Bad month number
        endsw
        


    7. Looping

      The looping constructs for the C shell are: foreach and while.



      1. foreach

        The foreach command is:

         foreach variable (list)
             command
         end
        
        #!/bin/csh
        foreach i  (`date`)
         echo $i
        end
        


      2. while

        The while command is:

          while (expression)
               command
          end
        
        #!/bin/csh
        while ( $#argv > 0 )
         echo $1
         shift
        end
        


      3. Like C syntax the while and foreach may use break and continue.

      4. Like C syntax the csh shell supports goto. The syntax is:

          goto label
        
          label : command
        

© Allan Kochis Last revision 1/4/2000