Interactive Usage Features of UNIX Shells with a Software Engineering-based Time Response Analysis

Ajeet K. Jain, Ashish Mangalampalli, B.M. Anathaakrishna

Variables that control the OS are called system variables or shell variables. Some shell variables, used in all the shells, are described below:

  • PATH: is a variable that instructs the shell about the route that it should follow to locate any executable file. To include the directory /home/anuj/commands in the search list, one needs to redefine the variable as follows:

$ PATH=$PATH:/home/anuj/commands

  • HOME: The directory that UNIX places the user when he logs in is called the home or login directory, and is available in the variable HOME.

$ echo $HOME
/home/anuj

The home directory of a user is set by adding a line pertaining to the user in the file /etc/passwd.
anuj:x:104:34::/home/anuj:/bin/bash

This file can only be modified by the system administrator, either manually or by using the useradd or usermod commands.

  • IFS: This variable contains a number of characters that are used as word separators in the command lines. The string normally consists of the space, tab and new line characters. All these characters are invisible.
  • PS1 and PS2: The shell has two prompts, PS1 and PS2. The user normally sees the primary prompt PS1 ($). PS2 contains the prompt (>) to be used in multi-line commands. The user can change the primary as well as the secondary prompts.

$ PS1=”C>”
C> _

  • SHELL: The SHELL variable decides the shell that the user sees on logging in. It is assigned after reading the last field pertaining to the user in /etc/passwd.
  • TERM: This variable indicates the terminal type being used.
  • LOGNAME: This variable contains the user’s login name.

Variables specific to the Korn and Bash Shells are:

  • HISTSIZE: The number of commands stored in the event list is determined by the HISTSIZE variable, which, if not set, stores the last 128 commands (500 in the Bash shell). Commands over multiple sessions can also be stored.
  • $_:  The $_ variable stores the last argument of the last command.

Variables specific to the Korn Shell include:
PWD:  The PWD variable, which gives the present working directory, can be used instead of the pwd command.

The C shell supports two types of shell variables, namely:

  • String-valued predefined shell variables: Variables whose value is set by the C shell; and
  • Set-valued shell variables: Variables whose value is irrelevant but their being set or unset is significant.

Some string-valued predefined shell variables are listed below:

  • cwd: This variable contains the current working directory.
  • status: This variable contains the return status of the previous command and is the C shell equivalent of the “?” variable in bash.

Predefined set-valued shell variables include:

  • echo: causes the C shell to display each command after first doing all the required substitutions.
  • ignoreof: forces the C shell to disregard all end-of-file characters that would otherwise cause the login shell to end.
  • noclobber: variable tells the C shell to abandon all commands using output redirection that would either destroy an existing file or append to the end of a non-existing file.
  • noglob: forces the C shell to ignore the file name expansion phase of the command execution. All file name arguments containing metacharacters are passed to the specified command unchanged.

%1 echo *
anuj2 data1 aba a.out
%2 set noglob
%3 echo *
*

  • verbose: causes the C shell to display commands as entered after doing all history substitutions and can also be set with the –v flag.

Table IV

Interactive Usage Features Available in Various Shells

Features

sh

Csh

Ksh

Bash

Aliases

POSIX

X

X

X

Alias Completion

-

-

-

X

Aliases take arguments

-

X

-

-

Automatically list choices for ambiguous complication

-

-

-

X

Cd path searched

 

X

X

X

Command Aliases

POSIX

X

X

X

Command editing (emacs)

POSIX

-

X

X

Command editing (vi)

POSIX

-

X

X

Command completion

POSIX

X

-

X

Built-in command completion

-

-

-

X

Command History

-

X

X

X

Command history completion

POSIX

-

X

X

Co-process support

POSIX

-

X

-

History substitution

POSIX

X

-

X

History expansion

-

-

-

X

Filename completion

-

X

X

X

Filename expansion

POSIX

-

X

-

Function completion

POSIX

-

-

X

Hostname completion

-

-

-

X

Incremental history searching

-

-

-

X

Job control (bg, fg … )

-

X

X

X

Multi-prompt command in history buffer

POSIX

-

X

X

Notify shell built-in

POSIX

X

-

X

One key completion

POSIX

X

-

X

pushd, popd command stack commands

-

X

-

X

Recursive command line scan

POSIX

X

-

X

Sub string selector

-

X

-

X

Variable completion

-

-

-

X

Variable expansion

-

-

-

X

*(…) Wildcards

POSIX

-

X

-

$(…) Command expression

POSIX

-

X

X

{…} Wildcards

-

-

-

X

X” indicates feature is present.                     “-” indicates feature is not present.

Time Response Analysis

A comprehensive time response analysis of various interactive usage features of all the four shells was done (Refer Appendices A and B). It was found that in general Korn shell gave the fastest output and C shell gave the slowest. Bourne and Bash shells gave outputs at nearly equal moderate speeds (Refer Figure 2). The shells are listed in descending order of their speeds taken in a generalized perspective:

  1. Korn Shell
  2. Bash Shell
  3. Bourne Shell
  4. C Shell

In fact, while using commands which are very resource intensive and in which the shell plays a minimal role (e.g. grep), the speeds of Bash shell comes very close to those of Korn shell (Refer to results of programs 2a. sh and 3a. sh in Appendix A). In some cases, even Bourne shell approaches the speeds of Korn shell.

Also, the usage of expr and bc commands to perform mathematical operations slows down the execution of the script because expr and bc are inherently very slow commands. Using bracket styled mathematical computations in Korn shell and direct mathematical computations in C shell speeds up the execution of commands to a great extent (Refer to results of programs 1c1.sh and 1k.ksh in Appendix A).

Overall, Korn and Bash shells are the best choices for fast and efficient execution of interactive usage commands.

Conclusions

From the above discussion, we can see that all the shells have many different interactive usage features to offer. Some of these are present in only one or two shells while a few are available in all the shells.

Bourne shell is the simplest and easiest to use and is good for a novice to UNIX. Also, it is universally available and comes bundled with nearly all flavors of UNIX. Bourne shell does not provide many of the advanced features that the other shells provide. Thus, it is not very useful for advanced users of UNIX.

C shell offers many advanced interactive usage features. However, these features are based on C language and are therefore quite complex and complicated to use. A user with prior experience of C language programming can exploit the features offered by this shell to the maximum. As such, users with very little or without any C language programming experience will find it very difficult to use the C shell. TC shell is a superset of C shell and is the best choice for ardent C shell and C language users.

Korn shell provides many of the interactive usage features that C shell provides and that too in an environment compatible with that of the POSIX and Bourne shells. In fact, the standard user command line and scripting interface is the Korn shell. It also provides some advanced interactive usage features not available in C shell. For example, for interactive use it provides the ability to edit the command line in a WYSIWYG fashion, i.e. by hitting the bang key (!) to recall a previous command and then edit the command as if the user was in an edit line mode. Ksh was patterned after the ex editor, the basis for the vi editor. Thus, Korn shell is easier and faster to learn, as it is compatible with Bourne shell. The only downside is that some of the advanced interactive usage features of Korn shell are complicated and even convoluted. For ardent Bourne shell users, Korn shell is the best option in case Bash shell is not available.

Bash shell emulates POSIX and Bourne shells and is ideal for every level of user, from beginner to expert. It provides simple and easy-to-use features as well as advanced ones. It also offers more completion and expansion options on the command line than C and Korn shells do. Bash is a superset of Bourne, Korn and C shells and has nearly all the interactive usage features provided by these shells. It is well documented with an extensive FAQ with tips on how to emulate common C shell features. Moreover, Bash contains a very powerful script to translate C shell aliases to Bash syntax and also to make semantic substitution. The only disadvantage of Bash shell is that it is not commonly available in most of the flavors of UNIX, excepting all flavors of Linux in which it is the default shell. Apart from this, the only major shortcoming, Bash shell is the best choice for all kinds of users, from beginners to experts and from ardent C shell users to ardent Bourne and Korn shell users.

The authors can be reached at amangalampalli2@yahoo.com.

References:

  1. Das, Sumitabha. UNIX: Concepts and Applications. Second Edition. New Delhi: Tata McGraw Hill Publishing Company Limited.
  2. Thomas, Rebecca, et al. Advanced Programmer’s Guide to UNIX System V. Berkeley: Osborne McGraw Hill.
  3. Unadkat, Chirag. UNIX Shells Bourne-Korn-C.
  4. Kanetkar, Yasvant. Shell Programming.
  5. Husain, Kumran, Shell Programming in 14 days.








}